久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      vuejs如何全局自定義變量

      方法:設(shè)置一個專用的的全局變量模塊文件,模塊里面定義一些變量初始狀態(tài),用“export default”暴露出去,在“main.js”里面使用“Vue.prototype”掛載到vue實(shí)例上面或者在其它地方需要使用時(shí),引入該模塊便可。

      vuejs如何全局自定義變量

      本教程操作環(huán)境:windows7系統(tǒng)、vue2.9.6版,DELL G3電腦。

      定義全局變量

      原理:

      設(shè)置一個專用的的全局變量模塊文件,模塊里面定義一些變量初始狀態(tài),用export default 暴露出去,在main.js里面使用Vue.prototype掛載到vue實(shí)例上面或者在其它地方需要使用時(shí),引入該模塊便可。

      全局變量模塊文件:

      Global.vue文件:

      <script> const serverSrc='www.baidu.com'; const token='12345678'; const hasEnter=false; const userSite="中國釣魚島";   export default   {     userSite,//用戶地址     token,//用戶token身份     serverSrc,//服務(wù)器地址     hasEnter,//用戶登錄狀態(tài)   } </script>

      使用方式1:

      在需要的地方引用進(jìn)全局變量模塊文件,然后通過文件里面的變量名字獲取全局變量參數(shù)值。

      在text1.vue組件中使用:

      <template>     <div>{{ token }}</div> </template>  <script> import global_ from '../../components/Global'//引用模塊進(jìn)來 export default {  name: 'text', data () {     return {          token:global_.token,//將全局變量賦值到data里面,也可以直接使用global_.token         }     } } </script> <style scoped>  </style>

      使用方式2:

      在程序入口的main.js文件里面,將上面那個Global.vue文件掛載到Vue.prototype。

       import global_ from './components/Global'//引用文件  Vue.prototype.GLOBAL = global_//掛載到Vue實(shí)例上面

      接著在整個項(xiàng)目中不需要再通過引用Global.vue模塊文件,直接通過this就可以直接訪問Global文件里面定義的全局變量。

      text2.vue:

      <template>     <div>{{ token }}</div> </template>  <script> export default {  name: 'text', data () {     return {          token:this.GLOBAL.token,//直接通過this訪問全局變量。         }     } } </script> <style scoped> </style>

      Vuex也可以設(shè)置全局變量

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號