这篇是我参加创作者训练营活动的最后一篇文章了。
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
// 默认为 false ,用于启动项目的时候提示信息,设置为false关闭提示
Vue.config.productionTip = true
//设置 mpType值
App.mpType = 'app'
// 生成 Vue
实例
const app = new Vue(App)
// 挂载组件
5.自动打包后的 dist 文件
<template>
<div class="indexContainer">
<img v-show="isShow" :src="userInfo.avatarUrl" alt="">
<button open-type="getUserInfo" @getuserinfo="handleGetUserInfo" class="btn"
v-show="!isShow">获取用户信息</button>
<p>hello {{userInfo.nickName}}</p>
<button @tap="toDetail">开启小程序之旅</button>
</div>
</template>
<script>
export default {
data(){
return {
isShow: false,
userInfo: {}
}
},
beforeMount(){
console.log('组件挂载之前')
// this.getUserInfo()
this.getUserInfo();
},
methods: {
handleGetUserInfo(msg){
console.log(this);
console.log(msg);
if(msg.mp.detail.rawData){
this.getUserInfo();
}
},
getUserInfo(){
// 获取用户信息
wx.getUserInfo({
success: (data) => {
this.isShow = true;
this.userInfo = data.userInfo
},
fail: () => {
}
})
},
toDetail(){
console.log('xxx');
wx.switchTab({
url: '/pages/list/main'
})
}
}
}
</script>
<style>
page {
background: gray;
height:100%;
}
.indexContainer {
display: flex;
flex-direction: column;
align-items: center;
}
.indexContainer img {
width:200rpx;
height:200rpx;
border-radius: 100rpx;
margin:100rpx 0;
}
.btn {
width:300rpx;
height:300rpx;
border-radius: 150rpx;
margin:100rpx 0;
line-height: 300rpx;
}
.indexContainer p {
font-size: 40rpx;
font-weight: bold;
margin:100rpx 0;
}
.indexContainer button {
background: #009ee0;
font-size: 32rpx;
}
</style>
import Vue from 'vue'
import Index from './index.vue'
const index = new Vue(Index)
index.$mount()
{
"navigationBarTitleText": "主页",
"navigationBarBackgroundColor": "#8ed145"
}
.4 注意事项 除了 Vue 本身的生命周期外,mpvue 还兼容了小程序生命周期,这部分生命周期钩子的来源于 微信小程序的 Page, 除特殊情况外,不建议使用小程序的生命周期钩子。