在我的项目中,当我从一个页面移动到另一个页面时<router-link>
,JavaScript 不起作用,请查看图像以了解我的意思。
第一次刷新页面时,一切正常,但是当我去例如测试页面(从localhost:8000/
到localhost:8000/test
)时,javascript不起作用,有人帮我吗?
我的app.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Vue App</title>
<link rel="stylesheet" type="text/css" href="{{mix('css/app.css')}}">
</head>
<body>
<div id="app">
<app></app>
</div>
<script src="{{mix('js/app.js')}}"></script>
<script src="{{mix('js/main.js')}}" async></script>
</body>
</html>
我的App.vue
<template>
<div class="root">
<Header/>
<router-view></router-view>
</div>
</template>
<script>
import Header from '@layout/Header';
export default
{
components:
{
Header
},
}
</script>
我的Header.vue
<template>
<header>
<router-link :to="{name: 'home'}"> Home </router-link>
<router-link :to="{name: 'test'}"> Teet </router-link>
</header>
</template>
我的app.js
require('./bootstrap');
import Vue from 'vue';
import VueRouter from 'vue-router';
import App from '@layout/App';
import {routes} from '@/routes';
Vue.use(VueRouter);
const router = new VueRouter({
mode: 'history',
routes
});
const app = new Vue({
el: '#app',
router,
// components: { App }
render : h => h(App)
});
我的routes.js
import Home from '@views/Home';
import Test from '@views/Test';
export const routes =
[
{
path: '/',
name: 'home',
component: Home
},
{
path: '/test',
name: 'test',
component: Test
},
]
我的webpack.mix.js
const mix = require('laravel-mix');
mix.webpackConfig({
resolve: {
extensions: [ '.js', '.vue' ],
alias: {
'@': path.resolve(__dirname, 'resources/js/'),
'@layout': path.resolve(__dirname, 'resources/js/layout'),
'@views': path.resolve(__dirname, 'resources/js/views'),
'@assets': path.resolve(__dirname, 'resources/assets/')
}
}
});
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
mix.copy('resources/assets/js/main.js', 'public/js/main.js');
如果你帮助我,我会很高兴。谢谢。