我如何在Vue.js中获取查询参数?
例如:
http://somesite.com?test=yay
我找不到获取数据的方法,是需要使用纯JS或某个库吗?
我如何在Vue.js中获取查询参数?
例如:
http://somesite.com?test=yay
我找不到获取数据的方法,是需要使用纯JS或某个库吗?
// from your component
console.log(this.$route.query.test) // outputs 'yay'
vue-router
,那么这将是一个重复的问题,因为他需要使用标准的JS方法。 - Jeffreturn next({name: 'login',query:{param1: "foo" },});
的操作是无效的。在登录组件内,prop“param1”未定义。 - hraynaud没有使用vue-router,分割URL
var vm = new Vue({
....
created() {
let uri = window.location.href.split('?');
if(uri.length == 2) {
let vars = uri[1].split('&');
let getVars = {};
let tmp = '';
vars.forEach(function(v) {
tmp = v.split('=');
if(tmp.length == 2)
getVars[tmp[0]] = tmp[1];
});
console.log(getVars);
// do
}
},
updated() {
},
....
另一种解决方案为https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLHyperlinkElementUtils/search:
var vm = new Vue({
....
created() {
let uri = window.location.search.substring(1);
let params = new URLSearchParams(uri);
console.log(params.get("var_name"));
},
updated() {
},
....
location.search
已准备好可用时,为什么要自己分割location.href
?请参考:https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search
例如:var querypairs = window.location.search.substr(1).split('&');
同时,通过'='分割查询名称-值对并不总是有效的,因为您也可以传递没有值的命名查询参数,例如:?par1=15&par2
在这种情况下,您的代码将引发异常,因为按'='分割会导致tmp
数组只有一个元素。 - Arthur.substring(1)
看起来是不必要的。 - Tofandel尝试这段代码
var vm = new Vue({
created() {
let urlParams = new URLSearchParams(window.location.search);
console.log(urlParams.has('yourParam')); // true
console.log(urlParams.get('yourParam')); // "MyParam"
},
});
window
不可用。这确实是一种糟糕的方法,迄今为止没有什么优雅的东西。请使用 Vue 生态系统提供的给定 API。Vue-router 是响应式的,并且考虑到了几个方面,如果创建它,那么有一个有效的原因。这不是为了好玩而做的事情。我希望没有人在这里使用 window.location.href
,否则每个人都会摧毁他们的 SPA。 - kissu为了帮助VueJS的新手更好地理解,以下是更详细的回答:
$route
实例保存有关当前路由的所有信息。该代码将仅在控制台记录传递到URL中的参数。(*Mounted类似于document.ready
,即在应用程序准备就绪时立即调用)代码本身:
<script src="https://unpkg.com/vue-router"></script>
var router = new VueRouter({
mode: 'history',
routes: []
});
var vm = new Vue({
router,
el: '#app',
mounted: function() {
q = this.$route.query.q
console.log(q)
},
});
<script src="https://unpkg.com/vue-router"></script>
。 - rustyxnew Vue({ router, ... })
不是有效的语法。 - Crescent Fresh另一种方法(假设您正在使用vue-router
),是将查询参数映射到路由器中的一个属性。然后,您可以像在组件代码中处理任何其他属性一样处理它。例如,添加这个路由;
{
path: '/mypage',
name: 'mypage',
component: MyPage,
props: (route) => ({ foo: route.query.foo }),
}
然后在您的组件中,您可以像平常一样添加该属性;
props: {
foo: {
type: String,
default: null,
}
},
然后它将作为this.foo
可用,您可以对其执行任何操作(例如设置观察程序等)。
this.$router.push({ name: 'mypage', query: { foo: 'bar' } })
- slf(截至2021年,vue-router版本为4)
import {useRoute} from "vue-router";
//can use only in setup()
useRoute().query.test
或者//somewhere in your src files
import router from "~/router";
//can use everywhere
router.currentRoute.value.query.test
或者import {useRouter} from "vue-router";
//can use only in setup()
useRouter().currentRoute.value.query.test
{ path: '/user/:id' }
,URL /user/123?a=b&c=d
将会有 $route.params == { id: "123" }
和 $route.query == { a: "b", c: "d" }
。 - phil294params
指的是 URL
中的参数,例如 /user/:username
。你可以看到这里的 username
是一个 params
,但是查询参数则像这样 /search/?q='new Vue js'
。正如你所见,new Vue js
在此处是一个查询参数。而且,文档中没有任何证据证明你的答案是正确的。请告诉我在文档的哪一行中你看到了这些内容? - Iman Shafieivue-router
,以下是如何操作的。import { useRoute } from 'vue-router'
export default {
setup() {
const route = useRoute()
console.log(route.query)
}
}
<script setup>
,直接跳过 setup()
部分。 - kissusomesite.com/something/123
在参数名为'id'的情况下(URL类似于/something/:id),尝试使用'123'作为参数:
this.$route.params.id
www.example.com?name=john&lastName=doe
new Vue({
el: "#app",
data: {
name: '',
lastName: '',
},
beforeRouteEnter(to, from, next) {
if(Object.keys(to.query).length !== 0) { //if the url has query (?query)
next(vm => {
vm.name = to.query.name;
vm.lastName = to.query.lastName;
});
}
next();
}
})
注意:在beforeRouteEnter
函数中,我们无法访问组件的属性,例如:this.propertyName
。这就是为什么我将vm
传递给next
函数的原因。这是访问Vue实例的推荐方式。事实上,vm
代表的就是Vue实例。
site.com/route?query=test
时,在 beforeRouteEnter(to, from, next)
方法中通过 to
参数访问 query
参数时返回 undefined
。有任何想法为什么会这样? - askepott