gpt4 book ai didi

change router view in laravel by vuejs(通过vuejs更改laravel中的路由器视图)

转载 作者:bug小助手 更新时间:2023-10-28 10:53:03 24 4
gpt4 key购买 nike



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。


更多回答

Just use inertiajs to do laravel with vue. even the official docs recommends it

只需使用inertiajs和vue来做laravel。即使是官方文件也建议

优秀答案推荐

app.js

App.js


...
import { createApp,h } from 'vue'
const app = createApp({
render: ()=>h(App)
});
...

app.blade.php

App.blade.php


...
<body>
<div id="app"></div>
</body>
...

更多回答

24 4 0
文章推荐: html - HTML复选框可以设置为只读吗?
文章推荐: html - 如何使用 CSS 设置复选框的样式
文章推荐: html - 何时使用 IMG 与 CSS 背景图像?
文章推荐: css - 如何将
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com