【问题标题】:Vue.js 2 router only loading component from navigation and not from URLVue.js 2 路由器仅从导航加载组件,而不是从 URL
【发布时间】:2017-07-12 12:49:41
【问题描述】:

从菜单中单击查看页面内容时,会加载正确的组件。但是,当我直接从 URL 访问它时,它不会。

这是母版页(加载菜单):

<template>
    <div class="row">
        <div class="col-md-3 col-sm-3 col-xs-3">
            <router-link to="/new-page" type="button" class="btn btn-primary btn-lg btn-block">New page</router-link>
            <div class="list-group sidebar">
                <router-link v-for="page in pages" class="list-group-item" :to="'/pages/' + page.slug">{{ page.menu_title }}</router-link>
            </div>
        </div>
        <div class="col-md-9 col-sm-9 col-xs-9">
            <router-view></router-view>
        </div>
    </div>
</template>

<script>
    export default {
        computed: {
            pages() {
                return this.$store.state.pages
            }
        },
        created() {
            this.$http.get('pages').then((response) => {
                this.$store.commit('setPages', response.body)
                console.log(response)
            }, (response) => {
                console.log("Error: " + response)
            })
        }
    }
</script>

这是根据单击的页面类型加载内容类型的内容。您可以使用多个模板重新加载不同的数据(那部分没问题)

<template>
    <div>
        <div class="loader" v-if="loading">
            <div class="bounce1"></div>
            <div class="bounce2"></div>
            <div class="bounce3"></div>
        </div>
        <div v-if="!loading">
            <vc-gig :content="content" v-if="content.type == 'gig'"></vc-gig>
            <vc-news :content="content" v-if="content.type == 'news'"></vc-news>
            <vc-home :content="content" v-if="content.type == 'home'"></vc-home>
            <vc-image :content="content" v-if="content.type == 'image'"></vc-image>
        </div>
    </div>
</template>

<script>
    import Gig from '../Gig.vue'
    import News from '../News.vue'
    import Home from '../Home.vue'

    export default {
        components: {
            'vc-gig': Gig,
            'vc-news': News,
            'vc-home': Home,
        },
        data() {
            return {
                loading: true,
                content: [],
            }
        },
        created() {
            this.getPageData
        },
        watch: {
            '$route': 'getPageData'
        },
        methods: {
            getPageData() {
                this.loading = true

                this.$http.get('pages/' + this.$route.params.pageSlug).then((response) => {
                    this.content = response.body
                    this.loading = false
                    console.log(response.body)
                }, (response) => {
                    console.log(response)
                })
            }
        }
    }
</script>

单击菜单时所有组件都正确加载(在第一个代码部分中),但如果我在浏览器中手动添加 URL,页面内容(第二个代码部分)将不会加载。

更新:这是我完整的routes.js 文件:

import Vue from 'vue'
import VueRouter from 'vue-router'

// Public
import Home from './views/Pages/Home.vue'

// Authentication
import Login from './views/Auth/Login.vue'
import Register from './views/Auth/Register.vue'
import Onboarding from './views/Auth/Onboarding.vue'
import ResetPassword from './views/Auth/ResetPassword.vue'

// Pages & Items
import Pages from './views/Pages/Layout/PageMaster.vue'
import Page from './views/Pages/Layout/PageSinge.vue'
import Item from './views/Pages/Layout/PageItem.vue'
import NewPage from './views/Pages/NewPage.vue'

// Options
import Options from './views/Options/Layout/OptionsMaster.vue'
import Themes from './views/Options/Themes.vue'
import Logo from './views/Options/Logo.vue'
import SocialMediaIcons from './views/Options/SocialMediaIcons.vue'
import WebsiteTitle from './views/Options/WebsiteTitle.vue'
import DomainName from './views/Options/DomainName.vue'
import Meta from './views/Options/Meta.vue'
import AnalyticsWebtools from './views/Options/AnalyticsWebtools.vue'

// My Account
import Account from './views/Account/Layout/AccountMaster.vue'
import Billing from './views/Account/Billing.vue'
import Details from './views/Account/Details.vue'
import Password from './views/Account/Password.vue'

Vue.use(VueRouter)

const Router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/login',
            name: 'login',
            component: Login,
            meta: {guest: true}
        },
        {
            path: '/register',
            name: 'register',
            component: Register,
            meta: {guest: true}
        },
        {
            path: '/reset-password',
            name: 'reset-password',
            component: ResetPassword,
            meta: {guest: true}
        },
        {
            path: '/onboarding',
            name: 'onboarding',
            component: Onboarding
        },
        {
            path: '/',
            name: 'home',
            redirect: 'pages/home',
            component: Home,
            meta: {auth: true}
        },
        {
            path: '/new-page',
            name: 'newpage',
            component: NewPage,
            meta: {auth: true}
        },
        {
            path: '/pages',
            name: 'pages',
            redirect: 'pages/home',
            component: Pages,
            meta: {auth: true},
            children: [
                {
                    path: ':pageSlug',
                    name: 'page',
                    component: Page,
                },
            ]
        },
        {
            path: '/pages/:pageSlug/:itemSlug',
            name: 'item',
            component: Item,
            meta: {auth: true}
        },
        {
            path: '/options',
            name: 'options',
            redirect: 'options/themes',
            component: Options,
            meta: {auth: true},
            children: [
                {
                    path: 'themes',
                    name: 'themes',
                    component: Themes
                },
                {
                    path: 'logo',
                    name: 'logo',
                    component: Logo
                },
                {
                    path: 'social-media-icons',
                    name: 'socialmediaicons',
                    component: SocialMediaIcons
                },
                {
                    path: 'website-title',
                    name: 'sitetitle',
                    component: WebsiteTitle
                },
                {
                    path: 'domain-name',
                    name: 'domain',
                    component: DomainName
                },
                {
                    path: 'meta-text-image',
                    name: 'meta',
                    component: Meta
                },
                {
                    path: 'analytics-webtools',
                    name: 'tools',
                    component: AnalyticsWebtools
                },
            ]
        },
        {
            path: '/account',
            name: 'account',
            component: Account,
            meta: {auth: true},
            children: [
                {
                    path: 'billing',
                    name: 'billing',
                    component: Billing
                },
                {
                    path: 'details',
                    name: 'details',
                    component: Details
                },
                {
                    path: 'password',
                    name: 'password',
                    component: Password
                },
            ]
        }
    ]
})

Router.beforeEach(function (to, from, next) {

    // User is authenticated
    if (to.matched.some(function (record) {
            return record.meta.guest
        }) && Vue.auth.loggedIn()) {
        next({
            path: '/pages'
        })
    } else {
        next()
    }

    // User not authenticated
    if (to.matched.some(function (record) {
            return record.meta.auth
        }) && !Vue.auth.loggedIn()) {
        next({
            path: '/login'
        })
    } else {
        next()
    }
})

export default Router

【问题讨论】:

  • 能不能也加个路由器配置文件?
  • 我已经更新了路由文件。具体路线是/pages:pageSlug,如果有帮助的话
  • 添加path: '/:pageSlug'有帮助吗?
  • 我试过'$route.params.pageSlug': 'getPageData',但没有任何区别。
  • 只是为了确保我们在同一页面上,在您的路线中,{ path: '/pages', name: 'pages', redirect: 'pages/home', component: Pages, meta: {auth: true}, children: [ { path: '/:pageSlug', //&lt;- [1] name: 'page', component: Page, }, ] } [1] 我们正在谈论的变化吗?

标签: javascript vue.js vue-router


【解决方案1】:

简单修复,您没有调用您创建的方法。

created() {
     this.getPageData
},

应该是

created() {
  this.getPageData()
},

也许使用像 eslint 这样的 linter 可以帮助避免这些错误。

http://eslint.org/

编码愉快!

【讨论】:

    猜你喜欢
    • 2019-07-04
    • 2017-09-09
    • 2016-05-04
    • 1970-01-01
    • 2019-12-31
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    相关资源
    最近更新 更多