消烦丹 發表於 2019-6-1 14:22:00

uni-app多种设置全局变量及全局变量重新赋值

<p>一、引用公共common.js(1、可在页面中直接引入common.js 2、在main.js中引用并挂载(这里是第二种))</p>
<div class="cnblogs_code">
<pre>export <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
    memberObj:{
      name:</span>'初始姓名'<span style="color: rgba(0, 0, 0, 1)">,
    },
    setMemberObj(data){
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.memberObj = Object.assign({},<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.memberObj,data)
    }
}</span></pre>
</div>
<p>(1)、在全局main.js中引用</p>
<div class="cnblogs_code">
<pre>import Vue from 'vue'<span style="color: rgba(0, 0, 0, 1)">
import App from </span>'./App'<span style="color: rgba(0, 0, 0, 1)">
import member from </span>'./common/common.js'<span style="color: rgba(0, 0, 0, 1)">

import store from </span>'./store'<span style="color: rgba(0, 0, 0, 1)">

Vue.config.productionTip </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">

Vue.prototype.$store </span>=<span style="color: rgba(0, 0, 0, 1)"> store
Vue.prototype.$member </span>=<span style="color: rgba(0, 0, 0, 1)"> member;
Vue.prototype.$enName </span>= 'ming'<span style="color: rgba(0, 0, 0, 1)">;

App.mpType </span>= 'app'<span style="color: rgba(0, 0, 0, 1)">

const app </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Vue({
    store,
    ...App
})
app.$mount()</span></pre>
</div>
<p>在普通页面获取全局变量,重新赋值</p>
<div class="cnblogs_code">
<pre>onShow:<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取全局设置的变量</span>
    <span style="color: rgba(0, 0, 255, 1)">this</span>.memberData = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$member.memberObj;
    console.log(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.memberData);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输出值{name:'初始姓名'}</span>
<span style="color: rgba(0, 0, 0, 1)">},
methods: {
bindLogin() {
    let that </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    let obj </span>=<span style="color: rgba(0, 0, 0, 1)"> {
      name:</span>'爱尚'<span style="color: rgba(0, 0, 0, 1)">,
      sex:</span>'男'<span style="color: rgba(0, 0, 0, 1)">
    }
    that.$member.setMemberObj(obj);
},
}<br>//再次在别的页面调用时内容已发生变化<br>console.log(this.$member.memberObj)<br>//</span>{name:'爱尚',sex:'男'<em id="__mceDel">}</em></pre>
<pre></pre>
</div>
<p>&nbsp;</p>
<p>二、通过vue的状态管理工具vuex管理全局变量</p>
<div class="cnblogs_code">
<pre>1<span style="color: rgba(0, 0, 0, 1)">、创建store文件,store.js
import Vue from </span>'vue'<span style="color: rgba(0, 0, 0, 1)">
import Vuex from </span>'vuex'<span style="color: rgba(0, 0, 0, 1)">

Vue.use(Vuex)

const store </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Vuex.Store({
    state: {
      memberData:</span>''<span style="color: rgba(0, 0, 0, 1)">,
      initName:</span>''<span style="color: rgba(0, 0, 0, 1)">
    },
    mutations: {
      copy(state,cont){
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">单一的改变某一个变量</span>
<span style="color: rgba(0, 0, 0, 1)">            console.log(state)
            console.log(cont)
            state.memberData </span>=<span style="color: rgba(0, 0, 0, 1)"> cont;
      },
      change(state,contObj){
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过传入的变量去改变对应的全局变量</span>
            let str =<span style="color: rgba(0, 0, 0, 1)"> contObj.str;
            let cont </span>=<span style="color: rgba(0, 0, 0, 1)"> contObj.cont;
            state </span>=<span style="color: rgba(0, 0, 0, 1)"> cont;
      },
    },
    actions:{
      copeFun:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(context,mData){
            context.commit(</span>'copy'<span style="color: rgba(0, 0, 0, 1)">,mData)
      },
      changeFun:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(context,obj){
            context.commit(</span>'change'<span style="color: rgba(0, 0, 0, 1)">,obj)
      }
    }
})

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> store</pre>
</div>
<p>2、在main.js中引入store.js</p>
<div class="cnblogs_code">
<pre>import Vue from 'vue'<span style="color: rgba(0, 0, 0, 1)">
import App from </span>'./App'<span style="color: rgba(0, 0, 0, 1)">

import store from </span>'./store'<span style="color: rgba(0, 0, 0, 1)">;

Vue.config.productionTip </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

Vue.prototype.$store </span>=<span style="color: rgba(0, 0, 0, 1)"> store;

App.mpType </span>= 'app'<span style="color: rgba(0, 0, 0, 1)">;

const app </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Vue({
    store,
    ...App
})
app.$mount()</span></pre>
</div>
<p>3、页面中获取需要使用的全局变量</p>
<pre><span>(1)、直接通过全局挂载的那种方式去获取(</span><em id="__mceDel">定义在计算属性中是为了方便实时的监听变量重新赋值</em><em id="__mceDel">)</em></pre>
<div class="cnblogs_code">
<pre>1<span style="color: rgba(0, 0, 0, 1)">、直接通过全局挂载的那种方式去获取

computed:{
   memberData:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$store.state.memberData;
   },
},</span></pre>
</div>
<pre>(2)、通过页面中引入vuex去获取</pre>
<div class="cnblogs_code">
<pre>import {mapState,mapMutations} from "vuex"<span style="color: rgba(0, 0, 0, 1)">;
computed:{<br>  //正常写法
    ...mapState({
      memberData:state </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> state.memberData,
      initName:state </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> state.initName,
    })<br><br>  //当变量名一致时(简写)<br>  //...mapState(['initName','memberData'])<br>
},</span></pre>
</div>
<p>3、重新赋值vuex中的全局变量</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">methods:{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">单一方法改变指定的变量</span>
changeMember:<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
    let mem </span>=<span style="color: rgba(0, 0, 0, 1)"> {
      name:</span>'爱尚丽明'<span style="color: rgba(0, 0, 0, 1)">,
      age:</span>'28'<span style="color: rgba(0, 0, 0, 1)">
    }
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.$store.dispatch('copeFun'<span style="color: rgba(0, 0, 0, 1)">,mem)
    },      
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过传入要改的变量名进行改变变量</span>
    changeMemberPub:<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
      let memberData </span>=<span style="color: rgba(0, 0, 0, 1)"> {
      name:</span>'爱尚'<span style="color: rgba(0, 0, 0, 1)">,
      age:</span>25<span style="color: rgba(0, 0, 0, 1)">
    }
    let $obj </span>=<span style="color: rgba(0, 0, 0, 1)"> {}
    $obj.cont </span>=<span style="color: rgba(0, 0, 0, 1)"> memberData ;
    $obj.str </span>= 'memberData'
    <span style="color: rgba(0, 0, 255, 1)">this</span>.$store.dispatch('changeFun'<span style="color: rgba(0, 0, 0, 1)">,$obj)
   }      
}</span></pre>
</div>
<p>&nbsp;</p>
<p>最后提示一点,如果重新赋值的全局变量没有实时去渲染可以尝试使用</p>
<p><span style="background-color: rgba(255, 255, 255, 1); color: rgba(255, 0, 0, 1); font-size: 18pt"><strong>this.$forceUpdate(); //强制数据渲染</strong></span></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/aishangliming/p/10959497.html
頁: [1]
查看完整版本: uni-app多种设置全局变量及全局变量重新赋值