gpt4 book ai didi

javascript - 路由时无法检索组件模板

转载 作者:行者123 更新时间:2023-12-01 00:09:29 25 4
gpt4 key购买 nike

我正在使用Vuejs。这是主要的js:

import Vue from 'vue'
import App from './App.vue'
import router from './router'

Vue.config.productionTip = false;


new Vue({
render: h => h(App),
router
}).$mount('#app')

这是路由器:

import Vue from 'vue';
import VueRouter from 'vue-router';
import ControlExcel from './components/ControlExcel';
import Login from "./components/Login";
import blog from './components/blog';
import file from './components/file';


Vue.use(VueRouter);

let routes = [
{
path: '/control-excel',
name: 'ControlExcel',
component: ControlExcel
}
,
{
path: '/loginn',
component: Login
}
,
{
path: '/blog',
name: 'blog',
component: blog
}
,
{
path: '/file',
name: 'file',
component: file
},


];

export default new VueRouter({
mode: 'history',
base: "",
routes
})

这个应用程序vue:

<template>
<div id="app">
<!-- <img alt="Vue logo" src="./assets/logo.png">-->
<!-- <HelloWorld msg="Welcome to Your Vue.js App"/>-->

<!-- <ControlExcel />-->

<!-- <component v-bind:is="component" />-->
<span>abcd</span>




<li class="nav-item">
<router-link class="nav-link" to="/control-excel">exce</router-link>
</li>
<li class="nav-item">
<router-link class="nav-link" to="/loginn">loginn</router-link>
</li>

<li class="nav-item">
<router-link class="nav-link" to="/blog">blog</router-link>
</li>

<li class="nav-item">
<router-link class="nav-link" to="/file">file</router-link>
</li>

</div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
// import Login from './components/Login.vue'
// import ControlExcel from "./components/ControlExcel";
// import Login from "./components/Login";
import file from './components/file';
import Vue from 'vue';

Vue.component('file',file);

// import DownloadExcel from './components/DownloadExcel'

export default {
name: 'app',
components: {
// Login,
// ControlExcel,

}
,
data() {
return {
// component: "Login"
}
}
}
</script>

在 localhost:8080,我看到那些:

abcd
exce
loginn
blog
file

当我单击其中任何一个时,URL 会发生变化。例如 http://localhost:8080/file对于文件,但它不带组件。

例如文件:

  {
path: '/file',
name: 'file',
component: file
},

该组件file.vue

<template>
<div class="blog">
<h1>fasfsasd</h1>
</div>
</template>
<script>
export default{
name:'file'
}

</script>
<style scoped>

</style>

这是目录:

src 
-components
--blog.vue
--ControlExcel.vue
--file.vue
--Login.vue
router.js
main.js
App.vue

为什么不能带模板呢?

有动态组件,但我不想使用它,而且它似乎不是最佳实践,如下所示:

<component v-bind:is=”currentComponent”></component>

https://blog.logrocket.com/how-to-make-your-components-dynamic-in-vue-js/

最佳答案

你的Vue-Router不会改变主组件,只是将匹配的URL的组件放入 <router-view></router-view> ,你没有。您需要将其添加到您的主要组件中。

例如这样:

<div id="app">
<span>abcd</span>
<ul>
<li class="nav-item">
<router-link class="nav-link" to="/control-excel">exce</router-link>
</li>
<li class="nav-item">
<router-link class="nav-link" to="/loginn">loginn</router-link>
</li>

<li class="nav-item">
<router-link class="nav-link" to="/blog">blog</router-link>
</li>

<li class="nav-item">
<router-link class="nav-link" to="/file">file</router-link>
</li>
</ul>

<router-view></router-view>

</div>

关于javascript - 路由时无法检索组件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60168675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com