开发者问题收集

VueJS 路由未定义

2018-01-24
784

我正在尝试弄清楚如何获取当前路线,因为它说 $route 未定义。我已经导入了该包并尽我所能应用它,但无法使其正常工作。

有人用过这个包并能看到我的问题吗?

<template>
<div class="row">
    <div class="col-12 col-md-6">
        <div class="form-group">
            <label class="control-label" for="start_date">Schedule Type</label>
            <select class="form-control" name="schedule_type" v-model="selected">
                <option value="1">Show All Projects</option>
                <option value="2">Show Scheduled Projects</option>
                <option value="3">Show Unscheduled Projects</option>
            </select>
        </div>
    </div>
    <div class="col-12 col-md-6">
        <div class="row">
           <div class="col-12 col-md-6">
                <div class="form-group" v-bind:class="{ 'hidden-mobile': isDisabled }">
                    <label class="control-label" for="start_date">Start Date (Start)</label>
                    <input class="form-control" type="date" id="start_date" name="range[start]" :disabled="isDisabled">
                </div>
            </div>
            <div class="col-12 col-md-6" v-bind:class="{ 'hidden-mobile': isDisabled }">
                <div class="form-group">
                    <label class="control-label" for="start_date_end">Start Date (End)</label>
                    <input class="form-control" type="date" id="start_date_end" name="range[end]" :disabled="isDisabled">
                </div>
            </div>
        </div> 
    </div>
 </div>
</template>

<script>

export default {
    data () {
        return {
            selected: 1,
            class: ''
        }
    },
    created() {
        console.log($route.query);
    }, 
    computed: {
        isDisabled() {
          return (this.selected == 1 || this.selected == 3);
        },
    }
}
</script>

更新了 App.js

window.Vue = require('vue');
var route = require('vue-router');
Vue.use(route);

const app = new Vue({
  el: "#app",
  components: {

 }
});
1个回答

在脚本中使用此代码:

this.$route.path

仅在模板中使用 $route ,例如:

  • 在“字符串插值”中,例如:{{ $route.path } }
  • 在属性绑定中,... .

对于您的情况,它将是:

created () {
    console.log(this.$route.path);
}
Moher
2018-01-24