J.D*_*Doe 23 javascript vue.js nuxt.js
其实我想知道在#NUXT.JS中存储常用组件方法的位置.
我试过的东西. - >在中间件(无用)中存储公共代码,因为根据我的知识,中间件只能处理对服务器的请求和响应.
methods: {
// states methods.
SwitchManager: function (__dataContainer, __key, __value) {
// stand alone debugger for this module.
let debug = __debug('computed:_3levelSwitch')
// debug showing function loaded.
debug('(h1:sizeCheck) creating switch..')
// switch.
switch (__key) {
// fake allow set value to true of given key
default:
this[__dataContainer][__key][__value] = true
break
}
return this[__dataContainer][__key][__value]
},
SizeCheck: function (__size) {
// stand alone debugger for this module.
let debug = __debug('tags:p')
// debug showing function loaded.
debug('(p:sizeCheck) checking..')
// init switch.
this.SwitchManager('pill', 'size', __size)
},
StateCheck: function (__state) {
// stand alone debugger for this module.
let debug = __debug('tags:h1')
// debug showing function loaded.
debug('(h1:sizeCheck) checking..')
// init switch.
this.SwitchManager('pill', 'state', __state)
}
},
created () {
// h1 tag size check
this.SizeCheck(this.size)
this.StateCheck(this.state)
}
Run Code Online (Sandbox Code Playgroud)
Sol*_*eno 62
我和普通的vue.js一样使用mixins.唯一的区别 - 对于全局混合 - 我将它们作为插件包含在内,但首先:
我会为我的mixins创建一个额外的文件夹.例如在一个/mixins/testMixin.js
export default {
methods: {
aCommonMethod () {}
}
}
Run Code Online (Sandbox Code Playgroud)
然后导入布局,页面或组件,并通过mixins对象添加它:
<script>
import commonMixin from '~/mixins/testMixin.js
export default {
mixins: [commonMixin]
}
</script>
Run Code Online (Sandbox Code Playgroud)
例如,在新文件中plugins/mixinCommonMethods.js:
import Vue from 'vue'
Vue.mixin({
methods: {
aCommonMethod () {}
}
})
Run Code Online (Sandbox Code Playgroud)
然后包含该文件 nuxt.config.js
plugins: ['~/plugins/mixinCommonMethods']
Run Code Online (Sandbox Code Playgroud)
之后,你可以在任何地方使用该方法并在那里调用它this.commonMethod().但是这里有来自vue.js文档的建议:
稀疏地小心使用全局mixin,因为它会影响每个创建的Vue实例,包括第三方组件.在大多数情况下,您只应将其用于自定义选项处理,如上例所示.将它们作为插件发送以避免重复应用也是一个好主意.
另一种可能性是在$ root和context中注入
| 归档时间: |
|
| 查看次数: |
11246 次 |
| 最近记录: |