0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

vueのrouterの設定

Last updated at Posted at 2021-01-13

vue-router

vue-routerを使ってみよう!備忘録

ファイル構成

スクリーンショット 2021-01-13 13.26.41.png

今回関係あるファイル

  • route.js
  • main.js
  • App.vue
  • pages
    • A.vue
    • B.vue
  • components
    • PageContent.vue

App.js

template内にrouter-linkを設置


<template>
  <div id="app">
    <router-link to="/a">go to A</router-link>
    <span></span>
    <router-link to="/b">go to B</router-link>
    <router-view></router-view>
  </div>
</template>

PageContent.vue


<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <p>ページの内容をそれぞれ表示するためのコンポーネント</p>
  </div>
</template>

<script>
export default {
  name: "PageContent",
  props: {
    msg: String
  }
};
</script>

A.vue


<template>
  <div id="app">
    <PageContent msg="A" />
  </div>
</template>

<script>
import PageContent from "@/components/PageContent";

export default {
  name: "A",
  components: {
    PageContent
  }
};
</script>

B.vue


<template>
  <div id="app">
    <PageContent msg="B" />
  </div>
</template>

<script>
import PageContent from "@/components/PageContent";

export default {
  name: "B",
  components: {
    PageContent
  }
};
</script>

route.js


import Vue from "vue";
import VueRouter from "vue-router";
import A from "@/pages/A";
import B from "@/pages/B";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    component: A,
  },
  {
    path: "/a",
    component: A,
  },
  {
    path: "/b",
    component: B,
  },
];

const router = new VueRouter({
  routes: routes,
});

export default router;

main.js


import Vue from "vue";
import App from "./App.vue";
import router from "./route.js";

Vue.config.productionTip = false;

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

参考🙏:
Vue Routerの書き方、使い方について解説
https://www.e-loop.jp/knowledges/14/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?