New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
20171109_vue_mixins用法 #63
Comments
也可以绑定全局组件,然后所有组件都可使用 // src/utils/mixin.js
let mixin = {
data () {
return {
// 本地上传图片地址(图片上传到服务端,地址应该是服务端地址,如果线上静态包和服务端不一致则需要配置线上地址)
// 也可以在 config/prod.env.js多配置两个参数
// baseURL: process.env.NODE_ENV === 'development' ? process.env.DEV_PATH : process.env.PROD_PATH
baseURL: process.env.NODE_ENV === 'development' ? '本地调试服务器路径/配置过proxy的路径' : '线上服务器路径'
}
},
methods: {
// 常用localStorage 方法
setStorage (key, objValue) {
window.localStorage.setItem(key, JSON.stringify(objValue))
},
getStorage (key) {
return JSON.parse(window.localStorage.getItem(key))
}
}
}
export default mixin // main.js
import mixin from '@/utils/mixin.js'
//启用全局mixin
Vue.mixin({
...mixin
}) |
有几个页面都需要任务状态,该状态是一个对象数组。 // src/utils/mixin.js
let mixin = {
data () {
return {
mixinTaskStatus: [],
mixinUploadUrl: process.env.NODE_ENV === 'development' ? 'aaa/fileUpload' : 'bbb/fileUpload'
}
},
methods: {
mixinFetchState () {
let data={}
......
// 判断本地存储(localStorage或sessionStorage)是否存在,
// 如果存在则赋值
// 如果不存在从服务端获取数据并赋值及存储在本地
this.mixinTaskStatus = data
}
}
export default mixin // src/main.js
// 启用全局mixn
import Vue from 'vue'
import mixin from '@/utils/mixin.js'
// 启用全局mixin
Vue.mixin({
...mixin
}) 具体使用: mounted () {
this.mixinFetchState()
} 在页面中就可以直接只用这个值了 <el-option :label="item.code"
v-for="item in mixinTaskStatus"
:key="item.id"
:value="item.name"></el-option> 也可以使用Vuex解决该问题,详见 #71 |
强烈不建议使用全局mixin
变通方法如下
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
看到过mixins但是一直没用过,后来发现他是这么用的
The text was updated successfully, but these errors were encountered: