代码之家  ›  专栏  ›  技术社区  ›  Logan Schwartz

在带有Vue的v-for循环中,将可调元素作为函数参数传递

  •  3
  • Logan Schwartz  · 技术社区  · 9 年前

    我还想知道我是否以正确的方式将参数传递给存储操作?

    //Side_bar.vue
    
     <template>
          <div id="sideBar">
    
            <ul>
              <li v-for='l in links'>
                <button v-on:click='d(l.title)'>{{l.title}}</button>
              </li>
            </ul>
    
          </div>
        </template>
    
    
        <script>
    
        export default {
          name: 'sideBar',
          data () {
            return {
              links: [
                {'title':'asset', 'valuesss':'ASSET'},
                {'title':'task', 'valuesss':'TASK'},
                {'title':'user', 'valuesss':'USER'}
              ]
            }
          },
          computed:{
            d(v){
              console.log(v)
    
              // update active table
              this.$store.dispatch('updateActiveTable',v)
    
            }
          }
    
        }
        </script>
    
        <style>
          li {
            list-style-type: none;
            padding-bottom: 5px;
          }
        </style>
    

    存储文件如下所示

    //store.js
    import Vue from 'vue'
    import Vuex from 'vuex'
    Vue.use(Vuex)
    
    const state = {
        activeTable: 'assets' // def view
    };
    
    const mutations = {
        setActiveTable(context,v){
            context.activeTable = v
        }
    };
    
    const getters={
        getActiveTable(context){
            //return active table
            return context.activeTable
        }
    
    };
    
     const actions={
        updateActiveTable(context,v){
            console.log(context)
            context.commit('setActiveTable',v)
        }
    }
    
    export default new Vuex.Store({
      state, 
      mutations,
      getters,
      actions  
    })
    

       <template>
      <div id="app">
        <sideBar></sideBar>
        <tableComponent></tableComponent>
      </div>
    </template>
    
    <script>
    import sideBar from './components/Side_bar'
    import tableComponent from './components/Table_component'
    
    export default {
      name: 'app',
      components:{
        sideBar,
        tableComponent
      }
    }
    </script>
    
    <style>
    #app {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    small {
      display: block;
      font-style: italic;
    }
    </style>
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   Bert Jeffrey Shen    9 年前

    代码定义 d 作为计算属性。

    这应该是一种方法。

    methods:{
      d(v){
        console.log(v)
    
        // update active table
        this.$store.dispatch('updateActiveTable',v)
    
      }
    }