I am learning Vuejs (v3.2.36) in laravel framework (v10.22). I want to change the codes in the Vuejs file. The problem is that the project does not work. Vuejs files are as follows.
我正在学习在laravel框架(v10.22)中使用Vuejs(v3.2.36)。我想更改Vuejs文件中的代码。问题是,这个项目并不奏效。Vuejs文件如下所示。
/resources/js/app.js
/Resources/js/app.js
import './bootstrap';
import Router from '@/router'
import store from '@/store'
import { createApp } from 'vue/dist/vue.esm-bundler';
const app = createApp({})
app.use(Router)
app.use(store)
app.component("my-component", Router)
app.mount('#app')
/resources/js/router/index.js
/Resources/js/router/index.js
import { createWebHistory, createRouter } from 'vue-router'
import store from '@/store'
const routes = [
{
name: "home",
path: "/",
component: () => import('@/components/Home.vue'),
meta: {
title: `..Home..`,
middleware: "guest",
requiresAuth: false,
}
},
{
name: "login",
...
},
...
{
path: "/dashboard",
component: () => import('@/layouts/Default.vue'),
meta: {
middleware: "auth",
requiresAuth: true,
},
children: [
{
name: "dashboard",
path: "/dashboard",
component: () => import('@/components/Dashboard.vue'),
...
}
]
}
]
const router = createRouter({
history: createWebHistory(),
routes,
})
router.beforeEach((to, from, next) => {
document.title = to.meta.title
if (to.meta.middleware == "guest") {
if (store.state.auth.authenticated) {
if (to.meta.pageLogin) {
next({name: "dashboard"})
}
}
next()
} else {
if (store.state.auth.authenticated) {
next()
return;
}
next({ name: "login" })
}
})
export default router
/resources/js/layouts/Default.vue
/Resources/js/Layout/Default.vue
<template>
<div>
<nav>
LoGo
</nav>
<main>
<router-view></router-view>
</main>
</div>
</template>
<script>
import {mapActions} from 'vuex';
export default {
name:"default-layout",
components: {
},
data(){
return {
}
},
methods:{
...mapActions({
signOut:"auth/logout"
}),
async logout(){
await axios.post('/logout').then(({data})=>{
this.signOut()
this.$router.push({name:"login"})
})
}
}
}
</script>
/resources/js/components/Home.vue
/Resources/js/Components/Home.vue
<template>
<div>
This is Home page.
</div>
</template>
<script>
import { mapActions } from 'vuex'
export default {
name:"home",
data(){
return {
}
},
methods:{
}
}
</script>
/resources/js/components/Dashboard.vue
/Resources/js/Components/Dashboard.vue
<template>
<div>
This is Dashboard page.
</div>
</template>
<script>
export default {
name:"dashboard",
data(){
return {
}
},
mounted() {
axios.get('api/user/1').then( (res) => {
this.user = res.data
})
}
}
</script>
The app.blade.php file is as follows (laravel).
/resources/views/layouts/app.blade.php
App.blade.php文件如下所示(Laravel)。/Resources/Views/Layout/app.blade.php
<!DOCTYPE html>
...
<body>
<div id="app">
<my-component></my-component>
</div>
</body>
</html>
Error:
错误:
[Vue warn]: Component is missing template or render function.
at <MyComponent>
at <App> runtime-core.esm-bundler.js:38:16
The error says the name MyComponent
is not defined.
错误提示未定义名称MyComponent。
更多回答
我是一名优秀的程序员,十分优秀!