Asked thg 1 10, 2021 10:57 SA 119 0 3
  • 119 0 3
0

hỏi về router vuejs

Share
  • 119 0 3

cho mình hỏi là mình muốn xử lý được route như sau: ví dụ đang ở trang chủ khi click vào icon home thì sẽ reload lại data ở trang. Thì sẽ config trong vuejs như thế nào? mình cảm ơn.

3 ANSWERS


Answered thg 1 11, 2021 12:57 SA
0

Bạn cần tìm hiểu về vue-router nhé, mình có ví dụ sẵn bên dưới

// Tạo file route.js
import VueRouter from 'vue-router'

import Home from ...
import Product from ...

export default new VueRouter({
    routes: [
    {
        path: '/home',
        component: Home,
        name: 'Home',
    },
    {
        path: '/product',
        component: Product,
        name: 'Product',
    },
]})
// Tạo file index.js
import Vue from 'vue'

import Home from ...
import router from './router

new Vue({
    router,
    template: '<Home />',
    components: { Home },
}).$mount('#root') // tao 1 index.html co div voi ID la root roi import file.js da duoc build
// index.html
<div id="root"></div>
<script src="../build.js"></script>
Share
Answered thg 1 11, 2021 3:56 SA
0
<router-view :key="$route.fullPath" />

Mình nghĩ bạn đang tìm kiếm thứ này

Share
Avatar flame @sven_9x
thg 1 11, 2021 7:41 SA

@tuananhbfs cái này mình thử rồi nhưng ko thấy đc

0
| Reply
Share
Avatar TAMIX @tuananhbfs
thg 1 11, 2021 9:47 SA

@sven_9x "data ở trang" là gồm những gì?

0
| Reply
Share
Avatar flame @sven_9x
thg 1 12, 2021 2:55 CH

data call từ api ở created đó b

0
| Reply
Share
Answered thg 1 12, 2021 8:06 SA
0

Mình chưa thử nhưng đang nghĩ đến cách sử dụng watch để reload lại data khi thấy route hiện tại thay đổi thành chính nó. Bạn thử theo hướng này xem.

export default {
  ...
  methods: {
    loadData () {
      // Load data ở trang chủ
    }
  },
  watch: {
    '$route.params.name'(to, from) {
      if (to === from) {
        this.loadData()
      }
    }
}
}
Share
Viblo
Let's register a Viblo Account to get more interesting posts.