【问题标题】:Vue.js Router change url but not viewVue.js 路由器更改 url 但不查看
【发布时间】:2018-08-02 12:02:30
【问题描述】:

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

[Vue 警告]:属性或方法“产品”未在实例上定义,但在渲染期间被引用。通过初始化该属性,确保该属性是反应性的,无论是在数据选项中,还是对于基于类的组件。

我的模板 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>'
});

感谢您的帮助。

【问题讨论】:

    标签: vue.js vue-component vue-router


    【解决方案1】:

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


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

    ChildComponent.vue

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

    【讨论】:

    • Very nice 是正确的,但现在我需要定义

      {{ product.title }}

      mmmmm...
      的“标题”
    • [Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性 'title'”
    • 在子组件标签上使用 v-bind:product="product" 绑定你的 product
    • 我可以试试,但是这里看不懂……抱歉
    • 您想通过接受答案来结束问题吗?
    【解决方案2】:

    第一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.

    【讨论】:

    • 很好,很好,很好,你太棒了!谢谢
    猜你喜欢
    • 2018-07-25
    • 2020-05-15
    • 2021-01-13
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    相关资源
    最近更新 更多