简体   繁体   English

无法在模板中渲染Vue-js

[英]Cannot render Vue-js in template

Is it possible to use Vue.js "stuff" inside of templates? 是否可以在模板中使用Vue.js“stuff”? I am trying to do this, but every time I try, nothing renders and a I get a worthless message in the console that says 我试图这样做,但每次我尝试,没有任何渲染,我在控制台中得到一个毫无价值的消息说

[Vue warn]: Error when rendering anonymous component: [Vue警告]:呈现匿名组件时出错:

Nothing gets rendered to the screen and there is no indication as to what Vue.js is having a problem with. 没有任何东西被渲染到屏幕上,也没有任何关于Vue.js出现问题的迹象。 Below is the code for the template: 以下是模板的代码:

   const list = {
        template: '<table><tr v-for="item in list"><td><router-link v-on:click.native="doSomething" v-bind:to="\'/item/\' + item.id">{{ item.title }}</router-link></td></tr></table>'
   }

   const viewItem = { 
        template: '<div>Not Implemented</div>'
   }     


   const router = new VueRouter({
        routes: [
          { path: '/item/:id', component: viewItem },
          { path: '/list', component: list }
        ]
    })

    const app = new Vue(
        {
            router: router,
            data: {
                list: [],
                test: "testing"
            },
            methods: { 
                 getList: //method to get data and populate list. This working correctly. 
                 doSomething: //method for fetching details.
            }
        }

Even doing something simple like {{ test }} in the template results in the same type of problem. 即使在模板中执行像{{test}}之类的简单操作也会导致相同类型的问题。 If I use some static HTML, things work alright. 如果我使用一些静态HTML,事情就可以了。 If you can't do this inside of a template, how can you accomplish non-static HTML? 如果您无法在模板中执行此操作,那么如何完成非静态HTML?

You may of course use Vue inside of templates. 您当然可以在模板中使用Vue。

The error you are receiving is because you incorrectly setting the data property. 您收到的错误是因为您错误地设置了data属性。 You have set list and test to the parent component, while you are trying to access them in your child list component. 当您尝试在子list组件中访问它们时,您已将listtest设置为父组件。 This produces an error while Vue tries to render the component, and thus the component is not rendered at all. 这会在Vue尝试渲染组件时产生错误,因此根本不会渲染组件。

To fix just change your list component to the following: 要解决此问题,只需将list组件更改为以下内容:

const list = {
  data () {
    return {
      list: [],
      test: "Now you should see me :)"
    }
  },
  template: '<table><tr v-for="item in list"><td><router-link v-on:click.native="doSomething" v-bind:to="\'/item/\' + item.id">{{ item.title }}</router-link></td></tr></table>'
}

You will also need to move your methods to the component where you are using them. 您还需要将方法移动到使用它们的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM