Vue.js 路由器更改 url 但未查看

Vue.js Router change url but not view

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

[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" 我不知道如何推送这个,请看我的代码。

HTML 列表 当我点击路由器时就可以了-link url 更改为:

例如

/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>'
});

感谢您的帮助。

你的道具应该是 props: ['product'] 而不是 props: ['product_id']


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

ChildComponent.vue

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

第一个activate props on the route:

var router = new VueRouter({
...
      path: '/product/:product_id',
      component: Product,
      name: 'product',
      props: true                    // <======= add this line
    },
...

现在 product_id 将在 Product 组件上设置。

所以,你想显示整个产品信息,但此时你只有product_id。解决方案是获取产品:

const Product = {
  props: ['product_id'],
  template: '#productDetail',
  data: function() {                              // <============ Added from this line...
    return {
      product: {}     // add {} so no error is thrown while the product is being fetched
    };
  },
  created: function() {
    var productId = this.product_id;
    // if there is a URL that fetches a given product by ID, it would be better than this
    this.$http.get('https://api.myjson.com/bins/17528x').then(function(response) {
      this.product = response.body.products.find(function (product) { return product.id == productId });
    }.bind(this));
  }                                               // <============ ...to this line.
}

检查上述解决方案的JSFiddle demo here


替代方案:将整个 product 作为 prop

params: 中传递 product(连同 product_id):

<router-link class="btn btn-default light-green darken-3" :to="{name: 'product', 
             params: {product_id: product.id, product: product}}">meer detail</router-link>
                                            ^^^^^^^^^^^^^^^^^^

Activate props on the route:

var router = new VueRouter({
...
      path: '/product/:product_id',
      component: Product,
      name: 'product',
      props: true                    // <======= add this line
    },
...

最后,添加product以便您可以使用它:

const Product = {
  props: ['product_id', 'product'],         // <======= added 'product' here
  template: '#productDetail'
}

Demo JSFiddle for this solution here.