百度了下,记录下来,只记录最简单的两种,其它的以后再说。
main.js文件里定义如下代码:
Vue.prototype.$version = '1.0.0';
Vue.prototype.$say = function() {
return 'hello'
}
这里定义的方法可以直接在页面模板里用{{$say()}}来调用,可以在方法里用this.$say()调用。但是变量却无法直接显示,如{{$version}},必须在data方法里赋值给变量,如:
export default {
data() {
return {
version: this.$version
}
}
}
然后就可以直接在模板里用{{version}}输出信息了
这个属性写在App.vue里,下面是个例子:
<script>
export default {
globalData: {
msg: 'hello',
say() {
return '1234'
}
},
onLaunch: function() {},
onShow: function() {},
onHide: function() {}
}
</script>
这种方式无法在页面模板代码里直接使用,只能在脚本里使用,通过getApp().globalData.msg
或者getApp().globalData.say()
来调用。