【问题标题】:How to add 'click' event listener or access 'ref' that's mentioned in JSON如何添加“click”事件侦听器或访问 JSON 中提到的“ref”
【发布时间】:2020-12-23 10:59:55
【问题描述】:

这是我的数据,看起来像这样:

cars: [
  {
    id: 1,
    desc: 'Description with <a ref="id1" @click="openModel('my-car')">a link</a> lorem ipsum continues.'
  }, {
    id: 2,
    desc: 'Description without link'
  }, {
    id: 3,
    desc: 'Description with <a ref="id3" @click="openAnotherModel('my-dashboard')">a link</a> lorem ipsum continues.'
  }
]

在我的模板中我可以做到:

<p v-for="item in cars" v-html="item"></p>

当然这肯定行不通:

<p v-for="item in cars">{{ item }}</p>

如何访问在我的 vue 实例中定义的方法/函数:

methods: {
  openModel(str) {
    console.log('openModel :>> ', str);
  },
  openAnotherModel(str) {
    console.log('openAnotherModel :>> ', str);
  },
},

【问题讨论】:

    标签: javascript json vue.js vuejs2


    【解决方案1】:

    评论后编辑。

    您可以从挂载的事件挂钩中访问您的链接,就像这样。

    new Vue({
      el: "#app",
      data: {
        cars: [
          {
            id: 1,
            desc: `Description with <a href="my-car">a link</a> lorem ipsum continues.`
          }, {
            id: 2,
            desc: `Description without link`
          }, {
            id: 3,
            desc: `Description with <a href="dash-board">a link</a> lorem ipsum continues.`
          }
        ]
      },
      methods: {
        dispatchLink(e){
          e.preventDefault();
          const target = e.target;
          const str = target.getAttribute('href');
          
          switch(str) {
              case 'my-car':
                this.openModel(str)
                break;
              case 'dash-board':
                this.openAnotherModel(str)
                break;
              // other link type ...
            }
        },
        openModel(str) {
          console.log('openModel :>> ', str);
        },
        openAnotherModel(str) {
          console.log('openAnotherModel :>> ', str);
        }
      },
      mounted(){
          const carsLinks = this.$el.querySelectorAll('p a');
          carsLinks.forEach(link => link.addEventListener('click', this.dispatchLink)       
          )
      }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    
    <div id="app">
      <p 
        v-for="car in cars" 
        :key="car.id" 
        v-html="car.desc"
        :id="car.id"
      >
      </p>
    </div>

    【讨论】:

    • 感谢您的尝试,我无权访问数据结构,文本也可以在链接标签结束后出现Description with &lt;a ref="id3" @click="openAnotherModel('my-dashboard')"&gt;a link&lt;/a&gt; lorem ipsum continues. 抱歉!您的解决方案没有帮助。再次感谢。
    • 我明白了......相应地编辑了我的答案,希望这会有所帮助。
    • 这只是一个条件(取决于链接href内容,有很多方法可以实现这一点,取决于您根据自己的需要进行调整...编辑了我的答案(添加了一个dispatch 函数和 switch 语句)
    • 感谢您的回答,即使我已经发布了我的答案,以防万一您对我的结果感到好奇:)
    • 就像我说的“有很多方法可以实现这一目标”,您的解决方案最适合您...很高兴我提供了帮助,祝您好运 ;)
    【解决方案2】:

    这是我提出的问题的最终结果。我没有弄清楚如何使@click="myFun('myData')" 工作,而是使用了data-image="myData"

    <template lang="pug">
      div(ref="cars")
        .row.no-gutters(v-for="(item, index) in cars" :key="index")
          p(v-html="item.desc")
    </template>
    
    <script>
    export default {
      data() {
        return {
          cars: [
            {
              id: 1,
              desc: 'Some text about <a href="#" onClick="return false;" class="jsOpenImgModal" data-image="/images/dash.png">dash</a> lorem ipsum continues.',
            }, {
              id: 2,
              desc: 'Description without link',
            }, {
              id: 3,
              desc: 'And, Some text about <a href="#" onClick="return false;" class="jsOpenImgModal" data-image="/image/front.png">front</a> lorem ipsum continues.',
            },
          ],
        };
      },
    
      mounted() {
        const imgModals = this.$refs.cars.querySelectorAll('.jsOpenImgModal');
    
        Object.keys(imgModals).forEach((i) => {
          const imgUrl = imgModals[i].dataset.image;
          imgModals[i].addEventListener('click', () => this.fnImgModals(imgUrl));
        });
      },
    
      methods: {
        fnImgModals(imgUrl) {
          console.log('dataset.image :>> ', imgUrl);
        },
      },
    };
    </script>
    

    注意:你们中很少有人会觉得这似乎是不现实的情况 任何开发人员都可以遇到。 cars 我创建的数据 以上只是为了展示我需要的东西,但我实际上需要这个 更复杂的数据和实际项目的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多