vue.js - 如何从url获取参数并在vue中显示
问题描述
我有一个网址:
http://localhost:8080/Currency?currency=RMB
我想获取货币参数,即RMB
在:
created(){
this.currencyParam = this.$route.query.currency;
console.log(curr: ${this.currencyParam});
}
我可以进去curr: RMB
,F12 - console
但F12 -Vue
我得到了currency:undefined
在我的模板中:
<template v-else>
<gateway
:currency="this.$route.query.currency"
/>
</template>
我收到一个错误:
渲染错误:“ TypeError:无法读取在和中找到的未定义的属性'$route'
F12 -Vue
我仍然得到currency:undefined
解决方案
您可以添加一个监视属性,该属性将允许您侦听查询参数中的更改
data () {
return {
currencyParam = null
}
},
watch: {
'$route.query.currency': function () {
if(this.$route && this.$route.query.currency) { // if not undefined
console.log(`curr: ${this.$route.query.currency}`);
this.currencyParam = this.$route.query.currency;
}
}
}
也像这样更改您的模板;
<template>
<gateway v-if="currencyParam" :currency="currencyParam" />
</template>
推荐阅读
- javascript - 为什么函数变量返回错误的计算?
- python - Python Bokeh,Google Maps API Patch Doc 错误
- sensenet - 在 Sense/Net 中将帖子分享到其他用户的墙
- r - 运行 Knitr Markdown 脚本后变量未在全局环境中加载
- matlab - 在 Octave 中为 fplot 设置线宽
- python - 使用 Beautiful Soup 选择 HTML 页面值
- php - 在 Laravel 刀片中 @extends 布局与条件如何传递数据
- c# - 覆盖基类的实现,以便从继承的基类函数调用也调用被覆盖的函数
- angular - 离子 3 指令。在 div 上添加自定义属性?
- python - Python 3 中的三向比较字符串