【发布时间】:2022-01-22 09:22:22
【问题描述】:
我有一个路线/网址:
例如。 http://localhost:8080/qr-codes
我想访问第一段
qr-codes
我该怎么做?
我曾经在 Laravel 中可以做这样的事情
请求::segment(1);
【问题讨论】:
标签: javascript vue.js vuejs2 vue-component vue-router
我有一个路线/网址:
例如。 http://localhost:8080/qr-codes
我想访问第一段
qr-codes
我该怎么做?
我曾经在 Laravel 中可以做这样的事情
请求::segment(1);
【问题讨论】:
标签: javascript vue.js vuejs2 vue-component vue-router
const getUrlPathSegments = () => (
(new URL(window.location.href)).pathname.split('/').filter(Boolean)
)
例如,当您在当前 stackoverflow 页面上调用该函数时,您会得到:
getUrlPathSegments() === ['questions', '70427242', 'vuejs-accessing-specific-url-segment']
getUrlPathSegments()[0] === 'questions'
【讨论】:
const str = 'http://localhost:8080/qr-codes';
const firstSegment = (new URL(str)).pathname.split('/')[1];
console.log(firstSegment);
Vue 演示:
new Vue({
el: "#app",
computed: {
path: function() {
const firstSegment = (new URL(window.location.href)).pathname.split('/')[1];
return `${firstSegment}/create`;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">{{path}}</div>
【讨论】: