我有一個路線/網址:
前任。http://localhost:8080/qr-codes
我想訪問第一段
qr-codes
我怎么做 ?
我曾經能夠在 Laravel 中做這樣的事情
請求::段(1);
uj5u.com熱心網友回復:
const getUrlPathSegments = () => (
(new URL(window.location.href)).pathname.split('/').filter(Boolean)
)
例如,當您在當前 stackoverflow 頁面上呼叫該函式時,您會得到:
getUrlPathSegments() === ['questions', '70427242', 'vuejs-accessing-specific-url-segment']
getUrlPathSegments()[0] === 'questions'
uj5u.com熱心網友回復:
使用URL.pathname和String#split:
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/388468.html
