vue+webpack新项目总结1

头部组件的  标题  根据不同的页面显示不同的标题

第一步:

  在store 里面初始化全局变量

// vuex 通过状态管理数据
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    // 公共
    comm: {
      loading: false,
      login: {
        memberId: '',
        userData: ''
      },
      indexConf: {
        isFooter: false, // 是否显示底部
        isBack: false,  // 是否显示返回
        title: '' // 标题
      }
    }
  },
  mutations: {
      /*修改header的信息*/
    changeIndexConf: (state, data) => {
      Object.assign(state.comm.indexConf, data)
    },
  },
  actions: {

  },
  getter: {

  }
})

export default store

第二步:

  在头部组件中添加计算属性,使得title可以动态变化

   import Header from '../components/header'
    
    export default{
        data: function () {
            return {
                title:'Markets'
            }
        },
        created () {//属性已绑定,dom未生成,一般在这里进行ajax处理以及页面初始化处理
            //改变store里面的变量
              this.$store.commit('changeIndexConf', {
                isFooter: false,
                isBack: true,
                title: 'Markets'
              })
        },
        components:{
            comHeader:Header
        }
    }

效果:

有关于watch,computed,methods 的区别和联系:

  参考文章:https://zhuanlan.zhihu.com/p/30584492

原文地址:https://www.cnblogs.com/rachelch/p/7744775.html