将仪表板和论坛组件从Vue.js的应用组件中分离出来。

huangapple go评论70阅读模式
英文:

Seperate Dashbord and Forum Component From App component in vuejs

问题

有没有办法在一个Vue实例上挂载多个组件。
我有我的管理面板和一个论坛页面,我不想在这些页面上显示页眉和导航。

这是我尝试过的:

import App from "./App.vue";
import Admin from "./Admin.vue";
import Forum from "./Forum.vue";

const app = new Vue({
    router,
    store,
    
    components: {
      App, Admin, Forum
    }
}).$mount("#app");

然后在我的 App.vue 中,我有其他子组件

<template>
<div>
  <div class="general-page">
    <AppHeader></AppHeader>
      <transition name="fade">
        <router-view></router-view>
    </transition>
    <AppFooter></AppFooter>
  </div>
</div>
</template>

<script>
import AppHeader from "./components/AppHeader";
import Login from "./components/Login.vue";
import Register from "./components/Register.vue";
import AppFooter from "./components/AppFooter.vue";

export default {
  components: {
    AppHeader,
    Login,
    Register,
    AppFooter
  }
};
</script>

在 Forum.vue 中

<template>
<div>
  <div class="forum-page">
    <ForumHeader></ForumHeader>
      <transition name="fade">
        <router-view></router-view>
    </transition>
    <ForumFooter></ForumFooter>
  </div>
</div>
</template>

<script>
import ForumHeader from "./components/ForumHeader";
import ForumFooter from "./components/ForumFooter.vue";

export default {
  components: {
    ForumHeader,
    ForumFooter
  }
};
</script>

Admin.vue

<template>
<div>
  <div class="admin-page">
    <AdminHeader></AdminHeader>
      <transition name="fade">
        <router-view></router-view>
    </transition>
    <AdminFooter></AdminFooter>
  </div>
</div>
</template>

<script>
import AdminHeader from "./components/AdminHeader";
import AdminFooter from "./components/AdminFooter.vue";

export default {
  components: {
    AdminHeader,
    AdminFooter
  }
};
</script>

论坛和管理面板的路由

{
    path: '/admin',
    name: 'Admin',
    component: Admin,
    meta: {
        requiresAuth: true
    },
    children: [
        { 
            path: '', 
            name: 'Profile',
            component: Profile
        },
        { 
            path: 'uploads',
            name: 'Uploads',
            component: Uploads,
            meta: {
                requiresCreatorAccess: true
            } 
        },
        { 
            path: 'add-post',
            name: 'AddPost',
            component: AddPost,
            meta: {
                requiresCreatorAccess: true
            } 
        }
    ]
},

{
    path: '/forum',
    name: 'Forum',
    component: Forum,
    children: [
        { 
            path: '', 
            name: 'Channel',
            component: Channel
        },
        { 
            path: 'threads',
            name: 'Threads',
            component: Threads
        },
        { 
            path: 'topic',
            name: 'Topic',
            component: Topic 
        }
    ]
}

如何动态地进入每个路由并将每个组件挂载到 el: #app ?

英文:

Is there a way to mount multiple components on a single vue instance.
I have my admin dashboard and a forum page and i don't want header and navigation to show up on these pages.

Here's what I've tried:

import App from &quot;./App.vue&quot;;
import Admin from &quot;./Admin.vue&quot;;
import Forum from &quot;./Forum.vue&quot;;

const app = new Vue({
    router,
    store,
    
    components: {
      App, Admin, Forum
    }
}).$mount(&quot;#app&quot;);

Then in my App.vue, I have other child components

&lt;template&gt;
&lt;div&gt;
  &lt;div class=&quot;general-page&quot;&gt;
    &lt;AppHeader&gt;&lt;/AppHeader&gt;
      &lt;transition name=&quot;fade&quot;&gt;
        &lt;router-view&gt;&lt;/router-view&gt;
    &lt;/transition&gt;
    &lt;AppFooter&gt;&lt;/AppFooter&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import AppHeader from &quot;./components/AppHeader&quot;;

import Login from &quot;./components/Login.vue&quot;;
import Register from &quot;./components/Register.vue&quot;;

import AppFooter from &quot;./components/AppFooter.vue&quot;;

export default {
  components: {
    AppHeader,
    Login,
    Register,
    AppFooter
  }
};
&lt;/script&gt;

In Forum.vue

&lt;template&gt;
&lt;div&gt;
  &lt;div class=&quot;forum-page&quot;&gt;
    &lt;ForumHeader&gt;&lt;/ForumHeader&gt;
      &lt;transition name=&quot;fade&quot;&gt;
        &lt;router-view&gt;&lt;/router-view&gt;
    &lt;/transition&gt;
    &lt;ForumFooter&gt;&lt;/ForumFooter&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import ForumHeader from &quot;./components/ForumHeader&quot;;

import ForumFooter from &quot;./components/ForumFooter.vue&quot;;

export default {
  components: {
    ForumHeader,
    ForumFooter
  }
};
&lt;/script&gt;

Admin.vue

&lt;template&gt;
&lt;div&gt;
  &lt;div class=&quot;admin-page&quot;&gt;
    &lt;AdminHeader&gt;&lt;/AdminHeader&gt;
      &lt;transition name=&quot;fade&quot;&gt;
        &lt;router-view&gt;&lt;/router-view&gt;
    &lt;/transition&gt;
    &lt;AdminFooter&gt;&lt;/AdminFooter&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import AdminHeader from &quot;./components/AdminHeader&quot;;

import AdminFooter from &quot;./components/AdminFooter.vue&quot;;

export default {
  components: {
    AdminHeader,
    AdminFooter
  }
};
&lt;/script&gt;

Routes for Forum and Admin

{
            path: &#39;/admin&#39;,
            name: &#39;Admin&#39;,
            component: Admin,
            meta: {
                requiresAuth: true
            },
            children: [
                { 
                    path: &#39;&#39;, 
                    name: &#39;Profile&#39;,
                    component: Profile
                },
                { 
                    path: &#39;uploads&#39;,
                    name: &#39;Uploads&#39;,
                    component: Uploads,
                    meta: {
                        requiresCreatorAccess: true
                    } 
                },
                { 
                    path: &#39;add-post&#39;,
                    name: &#39;AddPost&#39;,
                    component: AddPost,
                    meta: {
                        requiresCreatorAccess: true
                    } 
                }
            ]
        },

        {
            path: &#39;/forum&#39;,
            name: &#39;Forum&#39;,
            component: Forum,
            children: [
                { 
                    path: &#39;&#39;, 
                    name: &#39;Channel&#39;,
                    component: Channel
                },
                { 
                    path: &#39;threads&#39;,
                    name: &#39;Threads&#39;,
                    component: Threads
                },
                { 
                    path: &#39;topic&#39;,
                    name: &#39;Topic&#39;,
                    component: Topic 
                }
            ]
        },

How do I dynamically go to each route and mount each component on el: #app ?

答案1

得分: 1

以下是翻译好的部分:

CSS

/* 
假设应用程序的页眉和页脚具有 id 属性
根据您的需求进行更改
*/
#app-header,
#app-footer {
  display: none;
}

在 currentRoute 上使用 v-if

我们需要在这里执行一些操作。

  • 创建一个数据变量(showMe: true
  • 创建一个方法(evaluateShowMe
  • 为路由创建一个监视器('$route'()注意引号!

注意:随意重命名变量和函数以满足您的需求。

我们需要监视 $route,因为这位于 <router-view/> 之外,所以我们需要动态执行此操作,以便变量在每次路由更改时执行评估函数。

App.vue:

<template>
<div>
  <div class="general-page">
    <AppHeader
      v-if="showMe"
    ></AppHeader>
      <transition name="fade">
        <router-view></router-view>
    </transition>
    <AppFooter
      v-if="showMe"
    ></AppFooter>
  </div>
</div>
</template>

<script>
import AppHeader from "./components/AppHeader";

import Login from "./components/Login.vue";
import Register from "./components/Register.vue";

import AppFooter from "./components/AppFooter.vue";

export default {
  components: {
    AppHeader,
    Login,
    Register,
    AppFooter
  },
  data() {
    return {
      showMe: true
    }
  },
  methods: {
    evaluateShowMe() {
      // 获取路径中第一个和第二个斜杠之间的子字符串
      // 这将允许包含任何子路径
      // 注意:在我的测试中,第一个索引([0])为空,因此使用了一个([1])用于“filter”
      const entryPath = this.$router.currentRoute.path.split('/').filter((x,i) => i === 1);

      // 我们希望排除以下路径,即在这些路径上隐藏
      // 数组中应该只有一个项目,因此我们使用`[0]`提取
      return (entryPath[0] !== 'admin' || entryPath[0] !== 'forum');
    }
  },
  watch: {
    '$route'() {      
      this.showMe = this.evaluateShowMe();
    }
  }
};
</script>

希望这对您有所帮助!

英文:

Without changing any routing and template structure, you could use CSS to hide the app header, footer.

Another option may be to v-if the app header,footer to not render when on those routes using something like $router.currentRoute for matching.

CSS

/* 
Assuming app header and footer have an id attribute
  Change to your needs
*/
#app-header,
#app-footer {
  display: none;
}

v-if on currentRoute

We have to do a few of things here.

  • Create a data variable (showMe: true)
  • Create a method (evaluateShowMe)
  • Create a watcher for the route (&#39;$route&#39;()) Be aware of the quotes!

Note: Feel free to rename the variable and function to suit your needs.

We need to watch $route because this is outside of a &lt;router-view/&gt; so we need to do this dynamically so the variable performs the evaluator function every time the route changes.

App.vue:

&lt;template&gt;
&lt;div&gt;
  &lt;div class=&quot;general-page&quot;&gt;
    &lt;AppHeader
      v-if=&quot;showMe&quot;
    &gt;&lt;/AppHeader&gt;
      &lt;transition name=&quot;fade&quot;&gt;
        &lt;router-view&gt;&lt;/router-view&gt;
    &lt;/transition&gt;
    &lt;AppFooter
      v-if=&quot;showMe&quot;
    &gt;&lt;/AppFooter&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import AppHeader from &quot;./components/AppHeader&quot;;

import Login from &quot;./components/Login.vue&quot;;
import Register from &quot;./components/Register.vue&quot;;

import AppFooter from &quot;./components/AppFooter.vue&quot;;

export default {
  components: {
    AppHeader,
    Login,
    Register,
    AppFooter
  },
  data() {
    return {
      showMe: true
    }
  },
  methods: {
    evaluateShowMe() {
      // Get the substring of the path between first and second slash
      // This will allow to include any child pathing
      // NOTE: In my test the first index ([0]) was empty so used one ([1]) for the `filter`
      const entryPath = this.$router.currentRoute.path.split(&#39;/&#39;).filter((x,i) =&gt; i === 1);

      // We want to exclude the following paths i.e. hide when on these
      // There should only be one item in the array so we extract with `[0]`
      return (entryPath[0] !== &#39;admin&#39; || entryPath[0] !== &#39;forum&#39;);
    }
  },
  watch: {
    &#39;$route&#39;() {      
      this.showMe = this.evaluateShowMe();
    }
  }
};
&lt;/script&gt;


</details>



huangapple
  • 本文由 发表于 2020年1月6日 20:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定