简体   繁体   English

使用 vue.js 路由和混合的问题

[英]problem with using vue.js routing and mixin

Vue developers. Vue 开发人员。 Now I'm trying vuebnb tutorial and have a problem with routing and mixins.现在我正在尝试 vuebnb 教程,并且遇到了路由和 mixins 的问题。 I tried to assign data before entering the router using beforeRouteEnter guard, but it seems like my template is rendered before data assign.我尝试在使用 beforeRouteEnter 保护进入路由器之前分配数据,但似乎我的模板是在数据分配之前呈现的。 Following is the code I tried.以下是我尝试过的代码。

ListingPage.vue列表页面.vue

<template>
  <div>
    <img :src="'/' + images[1].img_path" />
  </div>
</template>
<script>
import { populateAmenitiesAndPrices } from "../js/helpers";
import routeMixin from '../js/route-mixin';

export default {
  mixins: [ routeMixin ],
  data() {
    return {
      title: null,
      about: null,
      address: null,
      amenities: [],
      prices: [],
      images:[],
    }
  },
  methods: {
    assignData({ listing, images }) {
      console.log('inside_component_before_assign');
      this.images = [...images];
      Object.assign(this.$data, populateAmenitiesAndPrices(listing));
      console.log('inside_component_after_assign');
    }
  },
  components: {
  }
};
</script>

route-mixin.js路由-mixin.js

import axios from 'axios';

function getData(to) {
    return new Promise((resolve) => {
        let serverData = JSON.parse(window.vuebnb_data);
        if (!serverData.path || to.path !== serverData.path) {
            axios.get(`/api${to.path}`).then(({ data }) => {
                resolve(data);
            });
        } else {
            resolve(serverData);
        }
    });
}
export default {
    beforeRouteEnter: (to, from, next) => {
        console.log('before_next');
        getData(to).then((data) => {
            next(component => {
                component.assignData(data);
            });
        });
        console.log('after_next');
    }
};

Here data is a object and it is like {listing: {...}, images: Array(5), path: "/listing/1} . Data fetch from the server is checked. But when I try to render ListingPage.vue, error is logged in console like this: *这里的数据是 object ,它就像{listing: {...}, images: Array(5), path: "/listing/1} 。检查从服务器获取的数据。但是当我尝试渲染列表页时。 vue,错误在控制台中记录如下:*

 [Vue warn]: Error in render: "TypeError: Cannot read property 'img_path' of undefined"

 found in
 ---> <ListingPage> at resources/assets/components/ListingPage.vue
       <App> at resources/assets/components/App.vue
         <Root>

Regardless of the error, the page is displayed successfully.不管错误如何,页面都显示成功。 Please help me to get rid of this error:请帮我摆脱这个错误:

控制台中的错误消息

From the reply on your comment I conclude your problem is solved by adding a v-if to your html tag?从您对评论的回复中,我得出结论,您的问题已通过在您的 html 标签中添加v-if来解决?

Changing your code to将您的代码更改为

 <img v-if="images[1]" :src="'/' + images[1].img_path" />

should work, as the async.应该工作,作为异步。 request is probably not resolved by the time, the page is compiled by vue. request 到时候可能还没有解决,页面是 vue 编译的。 For that have a look at the vue component lifecycle .为此,请查看 vue 组件生命周期

As the data attributes are reactive, your component will be visible and the value within will be updated couple ms later than it is compiled.由于数据属性是反应性的,因此您的组件将是可见的,并且其中的值将在编译后几毫秒更新。

I'd be glad if somebody could suggest another approach as do not if this is the best practice myself.如果有人可以建议另一种方法,如果这是我自己的最佳做法,我会很高兴。

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

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