Vue实例理解

index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>test</title>
  </head>
  <body>
    <!-- app:挂载Vue实例的的DOM节点-->
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

复制代码
main.js
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  /**
   * [el  该vue实例挂载到 index.html中 #app节点下]
   * @type {String}
   */
  el: '#app',
  /**
   * [router 路由:配置页面组件的匹配路径]
   * @type {String}
   */
  router,
  /**
   * [template 一个字符串模板做为 Vue 实例的标识使用。模板将会 替换 挂载的元素(index.html中的 #app节点)。挂载元素的内容都将被忽略,除非模板的内容有分发插槽]
   * @type {String}
   */
  template: '<App/>',
  /**
   * [components 声明在该Vue实例下的组件, 以供template使用]
   * @type {Object}
   */
  components: { App }
})
复制代码
router.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    }
  ]
})
复制代码

当访问http://127.0.0.1:8080/时渲染以前的代码为:html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>test</title>
  </head>
  <body>
    <!-- app:挂载Vue实例的的DOM节点-->
   <div id="app">
    <img src="./assets/logo.png">
   <!-- router-view 至关于容器, 渲染路由匹配到的路径,对应的组件 -->
    <div class="hello">
    <h1>{{ msg }}</h1>
    <h2>Essential Links</h2>
    <ul>
      <li><a href="https://vuejs.org" target="_blank">Core Docs</a></li>
      <li><a href="https://forum.vuejs.org" target="_blank">Forum</a></li>
      <li><a href="https://chat.vuejs.org" target="_blank">Community Chat</a></li>
      <li><a href="https://twitter.com/vuejs" target="_blank">Twitter</a></li>
      <br>
      <li><a href="http://vuejs-templates.github.io/webpack/" target="_blank">Docs for This Template</a></li>
    </ul>
    <h2>Ecosystem</h2>
    <ul>
      <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li>
      <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li>
      <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li>
      <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li>
    </ul>
  </div>
  </div>
  </body>
</html>
复制代码
相关文章
相关标签/搜索