如何在Vue中使用v-for传递函数的参数?

问题描述 投票:0回答:1

我试图通过使用v-for遍历数组项来将参数传递给函数。

<template>
  <v-app>
    <v-app-bar app>
      <v-app-bar-nav-icon @click="drawer = !drawer"></v-app-bar-nav-icon>
      <v-spacer></v-spacer>
      <h1 ref="y"></h1>
    </v-app-bar>
    <v-content>
      <router-view />
      <v-navigation-drawer v-model="drawer" class="x">
        <v-list-item
          v-for="item in items"
          :key="item.unidade"
          :to="item.link"
          :@click="change(item.method)"
        >{{item.unidade}}</v-list-item>
      </v-navigation-drawer>
    </v-content>
  </v-app>
</template>

<script>
export default {
  name: "App",

  data: () => ({
    items: [
      { unidade: "IPE", link: "/ipe", method: "IPE" },
      { unidade: "DCSI", link: "/dcsi", method: "DCSI" },
      { unidade: "RT", link: "/rt", method: "RT" }
    ],
    drawer: false
  }),
  methods: {
    change(val) {
      console.log(val);
      this.$refs.y.innerText = val;
    }
  }
};
</script>
<style lang="stylus" scoped>
.x {
  position: absolute;
}
</style>

我希望将arrray中的参数传递给change(val)方法,从而为每个v-list-item提供一个不同的事件侦听器。然后,我希望带有ref =“ y”的h1根据我单击的v-list-item更改其文本。但是到目前为止,我得到的浏览器错误是“渲染错误:“ TypeError:无法设置未定义的属性'innerText'””

vue-component vue-cli-3
1个回答
0
投票
代替设置<h1>的innerText,您可以将innerText绑定到反应变量。您可以在可以存储所选方法的数据中创建变量,然后使用{{}}语法将其绑定到innerText。以这种方式进行操作将更符合Vue最佳实践。让我告诉你我的意思。
© www.soinside.com 2019 - 2024. All rights reserved.