vue-router 在第一次进入页面,会运行所有 components 的 js

2019-03-15 12:45:53 +08:00
 TomVista

routerA.vue


<template>
    <div></div>
</template>

<script>
console.log('123')//看我.看我.看我:进入 /#/页面,这行代码会执行,但是 url 没有指向这个路由. 
export default {
    
}
</script>

app.vue

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script></script>

routes.js

export default new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: index
    },{
      path: '/routerA',
      name: 'routerA',
      component: routerA
    },
  ]
})

这个怎么解决,昨天下午才开始 vue-cli

1296 次点击
所在节点    问与答
3 条回复
noe132
2019-03-15 13:51:13 +08:00
这不是 vue 或 vue-router 的问题
建议你重新学习一下 javascript 基础以及 es module 相关的知识。vue SFC 会被编译成一个 es module,在 evaluation 的时候所有的代码都会执行。
http://es6.ruanyifeng.com/#docs/module
nigelvon
2019-03-15 13:54:06 +08:00
vue 异步组件了解一下。
TomVista
2019-03-15 14:06:36 +08:00
@noe132 明白了,非常感谢.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/544925

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX