vue中index.html、main.js、App.vue、index.js之前的關係以及載入過程--轉載
原文連結:https://blog.csdn.net/qq_34182808/article/details/86690193
作者:ONLY&YOU
簡介
專案部署完成後的專案結構以及解釋如下圖所示
專案執行
專案的執行入口index.html
為什麼index.html是專案的入口以及為什麼index.html載入後會繼續載入main.js、App.vue、index.js,以及他們之間的關係是如何聯絡起來的呢,這塊的配置檔案位於build資料夾下,包括webpack.dev.conf.js等,感興趣的可以瞭解下。通過專案的配置檔案,可以載入執行我們的index.html檔案以及自動關聯vue相關的模組。
首先我們來看一下index.html中的內容
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>y</title> </head> <body> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
在body體中只有一個div標籤,其id為app,這個id將會連線到src/main.js內容,接著我們看一下main.js中的主要的程式碼
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' })
在main.js中,新建了一個vue例項,並使用el:#app連結到index.html中的app,並使用template引入元件
<template>
<div id="app">
<img src="./assets/logo.png">
<router-view/>
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
看一下App.vue中的內容,是一個標準的App.vue模板的形式,包含了、、三部分,從template標籤中可以看到,使用img標籤載入了vue的影象,也就是我們使用npm run dev執行vue專案後看到的影象,那麼影象下面的內容是從哪裡渲染出來的呢?
我們注意到,標籤下,除了標籤外,還有 在index.js的程式碼中,建立了路由相關的內容,也就會渲染到app.vue下面的 在helloworld.vue的template中,我們可以看到介面上渲染的一些連線等內容。到此,這個頁面的載入渲染過程結束了。 總結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
}
]
})
<template>
<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>
</template>
通過上述過程,我們可以看到專案載入的過程是index.tml->main.js->app.vue->index.js->helloworld.vue。這裡只是對我們執行專案後,如何出現首頁面做了簡單的解釋,對具體的實現沒有進行分析。