VueJS Ресурс перезагрузить контент

Файл ресурсов помощник / json.json

{
  "content": {
    "content_body": "<a href='#' v-on:click.prevent='getLink'>{{ button }}</a>",
    "content_nav": "",
  }
}

Vue main.js файл

new Vue({
    el: 'body',

    data: {
        text: 'Lorem sss',
    },

    methods: {
        getLink: function(){
            this.$http.get('http://localhost/vuejs/helper/json.json').then((resp) => {

                this.$set('text', resp.data.content.content_body);

            }, (resp) => {
                console.log('error');
            })
        }
    }
})

Вывод: не рендерер

<a href="#" v-on:click.prevent="getLink">{{ button }}</a>

Событие не работает при нажатии кнопки. Данные не могут быть загружены.

1 ответ

Решение

Vue.resourse не имеет отношения к этой проблеме, потому что HTML-строка из JSON не скомпилирована.

Вот небольшой тест на основе вашего примера:

<body>
    <a href='#' v-on:click.prevent='getLink' v-text="button"></a>
    <div v-el:sample></div>
</body>
var test = new Vue({
  el: 'body',

  data: {
    button: 'Lorem sss',
  },

  methods: {
    getLink: function(){
      var r = Math.floor(Math.random() * (4 - 1)) + 1;
      this.$set('button', ['','btn1','btn2','btn3'][r] );
    },

    getCompiled: function() {
      $(this.$els.sample).empty()
      var element = $(this.$els.sample).append("<a href='#' v-on:click.prevent='getLink'>{{ button }}</a>");
      this.$compile(element.get(0));
      $(this.$els.sample).prepend('<p>Compiled button:</p>')
    }
  },

  ready: function() {
    this.getCompiled();
  }
})

jsfidle

Другие вопросы по тегам