javascript - Vue.js 2 路由器只从导航而不是从 URL 加载组件

标签 javascript vue.js vue-router

当从菜单中单击以查看页面内容时,正确的组件将加载。但是,当我直接从 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

最佳答案

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

created() {
     this.getPageData
},

应该是

created() {
  this.getPageData()
},

也许使用 eslint 等 linter 也有助于避免这些错误。

http://eslint.org/

编码愉快!

关于javascript - Vue.js 2 路由器只从导航而不是从 URL 加载组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42392034/

相关文章:

javascript - react router v4 不呈现任何其他路由

javascript - 开始时间和结束时间验证

javascript - 在 vue 路由中匹配查询参数

javascript - 组件渲染后,Vue.js 将 click 事件绑定(bind)到 v-html 内的 anchor 元素

nuxt.js - 如何使用vue路由器将props传递到nuxt错误页面?

javascript - 无法显示注册页面 vue.js

javascript - Vue-router 只显示基本路由

javascript - 简单的 SOAP 客户端,用于使用 javascript 或 java 来使用 Web 服务

javascript - 在 React 中找不到模块

webpack - 使用 Webpack 2 延迟加载 Vue 组件