Vue.js 路由器更改 url 但不更改 View

标签 vue.js vue-component vue-router

我在控制台中看到这个错误:

[Vue warn]: Property or method "product" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

我的模板 id="productDetail"未收到模板 id="product"的属性“product”,我不知道如何推送此内容,请参阅我的 cod。

HTML 列表 当我单击路由器链接时,网址更改为:

/product/iphone-x-64-gb 例如。

<template id="product" functional>
<div class="wrapper">
    <ul class="row">
        <li v-for="(product, index) in products" class="col l4 m6 s12">
            <div class="card-box">
                <div class="card-image">
                    <img :src="product.images" :alt="product.images" class="responsive-img"/>
                </div>
                <div class="card-content">
                    <h3><a href="#">{{ product.brand }}</a></h3>
                    <span class="price-used"><i class="used">{{ index }} gebrauchte Roomba 651</i></span>
                </div>
                <div class="card-action row">
                    <span class="col s6 price"><span>{{ product.price }}</span>
                </div>
                <div>
                    <router-link class="btn btn-default light-green darken-3" :to="{name: 'product', params: {product_id: product.id}}">meer detail</router-link>
                </div>
            </div>
        </li>
    </ul>
</div>

HTML 产品详细信息(未收到“产品”)

<template id="productDetail" functional>
<div class="row">
    <div class="col s12 m6">
        <img src="images/iphone-8-64-gb.jpg" alt="product.images" class="responsive-img"/>
    </div>
    <div class="col s12 m6">
        <h3>{{ product.title }}</h3>
        <h5>{{ product.price }}<h5>
        <div class="col s12 m6">
            <a class="waves-effect waves-light btn light-green darken-3"><i class="material-icons left">add_shopping_cart</i>kopen</a>
        </div>
        <div class="col s12 m6">
            <router-link class="btn btn-default light-green darken-3" :to="{path: '/'}">
                <span class="glyphicon glyphicon-plus"></span><i class="material-icons left">arrow_back</i>terug
            </router-link>
        </div>
    </div>
</div>

.JS

var List = Vue.extend(
{
    template: '#product',
    data: function ()
    {
        return {products: [],};
    },
    created: function()
    {
        this.$http.get('https://api.myjson.com/bins/17528x').then(function(response) {
            this.products = response.body.products;
        }.bind(this));
    },

});

const Product =
{
    props: ['product_id'],
    template: '#productDetail'
}

var router = new VueRouter(
{
    routes: [
    {path: '/', component: List},
    {path: '/product/:product_id', component: Product, name: 'product'},
    ]
});


var app = new Vue(
{
    el: '#app',
    router: router,
    template: '<router-view></router-view>'
});

感谢您的帮助。

最佳答案

您的 Prop 应该是 props: ['product'] 而不是 props: ['product_id']


<parent-component :product="product"></parent-component>

ChildComponent.vue

export default {
  name: 'child-component',
  props: ['product']
}

关于Vue.js 路由器更改 url 但不更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48930773/

相关文章:

javascript - 在组件外使用 VueI18n 的问题

vue.js - Vue js v-model 不同的复选框

typescript - 如何在VueJS 3.0 中使用defineComponent 注册本地组件

vue.js - vue树形导航(插件)如何映射路线

vue.js - 如何删除未安装组件的组件样式?

javascript - VueJS : Reset/Clear value from input

vue.js - 使用 Vue.js 和 DataTable(jquery 插件)

html - 如何使具有相同值的单选按钮可重复使用?

vue.js - 将 Cypress 与 NuxtJS 一起使用,并在开始 e2e 测试之前等待服务器启动

laravel - 生产模式编译报错 "is not a function"Vue Laravel Mix