【发布时间】:2020-12-18 11:15:13
【问题描述】:
我正在尝试构建一个简单的 Vue 应用程序,其中路由器链接将基于从服务器接收到的数据,数据是这样的。
id: 1
path: "category_image/About.jpg"
slug: "about"
subtitle: null
title: "About Us"
url: "http://archeoportal.loc/page/about"
现在我要实现的是动态路由器链接元素,如果url 字段不为空,则将使用window.location.href,否则我希望它只是一个路由器链接。目前我所做的不起作用,它不断抛出像TypeError: Cannot read property 'redirect' of undefined 这样的错误。这是我的 Vue 文件的样子
<router-link
:to="this.redirect(category.url !== null ? category.url : category.slug, category.url !== null ? true : false)"
class="grid-item"
v-bind:key="category.id"
v-for="category in this.categories"
>
<div class="category-title py-4">
<h2>{{ category.title }}</h2>
<p>{{ category.description }}</p>
</div>
<img :src="`/storage/${category.path}`" />
</router-link>
如您所见,我正在使用自定义方法来处理我的方法中的内容,类似于这样
methods:{
redirect(url, window){
if(window == true){
window.location.href = url;
}else{
this.router.push('url');
}
}
}
但是我的 Vue 应用程序崩溃并且没有显示任何内容,有什么方法可以实现吗?
【问题讨论】:
标签: javascript vue.js vue-router