# Vue router

1. `yarn add vue-router`
2. 記得在 index.html 加上如下

```javascript
    <div id="app">
      <router-view></router-view>
    </div>
```

3\. main.js

```javascript
import Vue from 'vue'
import App from '@/App.vue'
import Test from "@/components/Test.vue";
import VueRouter from 'vue-router'

Vue.use(VueRouter)
Vue.config.productionTip = false

const routes = [
  { path: '/', component: App },
  { path: '/foo', component: Test },
]
const router = new VueRouter({
  mode: 'history',
  routes 
})
new Vue({
  router,
}).$mount('#app')

```

4\. 點擊換頁面

```javascript
<template id="test">
  <div>
    <button v-on:click="gotoHome">test</button>
  </div>
</template>

<script>
export default {
  name: "test",
  mounted: () => {
    console.log("test mounted");
  },
  methods: {
    gotoHome() { // 不能用 arrow function
      this.$router.push("/");
    }
  }
};
</script>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easonwang.gitbook.io/web_advance/vue/vue-router.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
