javascript - vue 在template 写 v-for 问题
天蓬老师
天蓬老师 2017-04-11 09:45:22
[JavaScript讨论组]

先上代码

<router-view></router-view>

const Article = {
    template:'<p>article</p>'
}

const News = {
    template:'<ul>\
                <li class="news" v-for="item in items">\
                    <p class="cover" v-bind:style="{backgroundImage:\'url(\'+item.bg+\')\'}"></p>\
                    <h4 class="title">{{ item.title }}<span class="up_time">{{ item.date }}</span></h4>\
                    <h5 class="detail">{{ item.detail }}</h5>\
                    <p class="border_bottom"></p>\
                </li>\
            </ul>\
'}

const router = new VueRouter({
    routes:[
        {path:'/article', component: Article},
        {path:'/',component: News}
    ]
})

var box = new Vue({
    router,
    el: "#box",
    data: {
    items: []    
    },
    ...

代码大概就是这样,想通过vue-router判断不同路径在router-view中渲染不同内容,但是像上面这么写会报错:

[Vue warn]: Property or method "items" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. 
(found in anonymous component - use the "name" option for better debugging messages.)

不太清楚这个问题是什么意思,希望大神们给点解决办法,或者用其他路由方法也可以 只要跳转时不重复刷新页面就好。

还有个问题就是,vue-router 的history模式 说是需要后端配置否则会404 我按照官网的写法用node中间件把跳转路径都重新定向到当前页面了,虽然跳转是解决了,但是每次跳转都重新载入页面并不是局部刷新了 有什么解决办法吗?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
PHP中文网

News引用的items应该是News这个组件的数据,而你的items是注册在根实例里的。

试试这样:

const News = {
    template:'<ul>\
                <li class="news" v-for="item in items">\
                    <p class="cover" v-bind:style="{backgroundImage:\'url(\'+item.bg+\')\'}"></p>\
                    <h4 class="title">{{ item.title }}<span class="up_time">{{ item.date }}</span></h4>\
                    <h5 class="detail">{{ item.detail }}</h5>\
                    <p class="border_bottom"></p>\
                </li>\
            </ul>\
',
    data: function () {
        return {
            items: []
        }
    }
},
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号