簡述: 本節(jié)主要記錄一個(gè) Vue 的全局變量定義方式,方法比較邪惡
定義聲明
main.js
import './common/commonJS/jsencrypt'
import {encrypt,decrypt} from './common/commonJS/RSA'
...
...
new Vue({
el: '#app',
router,
components: {
App
},
template: '<App/>',
data () {
return {
// 這里就是你的全局變量(屬性)了,我這里三個(gè)屬性值都是 ```function```,
// 當(dāng)然你想寫什么都行
encrypt: encrypt,
decrypt: decrypt,
MD5: MD5,
// ES6寫法
// encrypt,
// decrypt,
//MD5
}
}
})
訪問
other.vue
methods: {
yourFunction () {
this.$root. encrypt('encrypt 的參數(shù)');
this.$root. decrypt('decrypt 的參數(shù)');
this.$root. MD5;
// 也許你不想在 new Vue 的時(shí)候聲明一堆的屬性,
// 還可以這么寫,在其他 vue 組件里,你依然可以通過 Vue.$root.yourVariable 訪問
this.$root.yourVariable = otherVariable;
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。