V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
TomVista
V2EX  ›  问与答

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

  •  
  •   TomVista · 2019-03-15 12:45:53 +08:00 · 1292 次点击
    这是一个创建于 1859 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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

    noe132
        1
    noe132  
       2019-03-15 13:51:13 +08:00   ❤️ 1
    这不是 vue 或 vue-router 的问题
    建议你重新学习一下 javascript 基础以及 es module 相关的知识。vue SFC 会被编译成一个 es module,在 evaluation 的时候所有的代码都会执行。
    http://es6.ruanyifeng.com/#docs/module
    nigelvon
        2
    nigelvon  
       2019-03-15 13:54:06 +08:00   ❤️ 1
    vue 异步组件了解一下。
    TomVista
        3
    TomVista  
    OP
       2019-03-15 14:06:36 +08:00
    @noe132 明白了,非常感谢.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2229 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:13 · PVG 00:13 · LAX 09:13 · JFK 12:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.