浅谈vue中index.html、main.js、App.vue、index.js之前的关系以及加载过程

前序


承接上一遍“通过webpack构建vue项目”构建的项目文件,简单阐述一下当我们构建完成后,vue项目中的index.html、main.js、App.vue、index.js的运行加载过程,以及首界面是如何出现的,逐步了解vue项目,针对刚开始接触vue,不知道vue项目如何加载的小白,大神请绕过。

简介


项目部署完成后的项目结构以及解释如下图所示

项目运行


项目的运行入口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引入组件<app>和路由相关的内容(具体的涉及到vue的语法规则,如果不理解的先记下来吧,继续往后看,等了解vue的相关内容后,可能会更清晰)。也就是说通过main.js我们关联到App.vue组件,接着,我们继续看一下App.vue组件中的内容。

<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></template>、<script></script>、<style></style>三部分,从template标签中可以看到,使用img标签加载了vue的图像,也就是我们使用npm run dev运行vue项目后看到的图像,那么图像下面的内容是从哪里渲染出来的呢?

 

我们注意到,<template>标签下,除了<img>标签外,还有<router-view>标签,<router-view>标签将会把路由相关内容渲染在这个地方,接下来,我们看一下路由的内容有哪些,在哪里出现的。其实,这个文件位于src/router/index.js中,我们看一下index.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
    }
  ]
})

在index.js的代码中,建立了路由相关的内容,也就会渲染到app.vue下面的<router-view>中。在index.js中,将helloworld组件发布为路由,换句说,index.js在这里就是将helloword发布为路由,以在图片下面进行展示helloword内容,接下来我们再看看components/helloword中的内容是啥(由于里面的内容比较多,这里我们只截取了template中的内容)。

<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>

在helloworld.vue的template中,我们可以看到界面上渲染的一些连接等内容。到此,这个页面的加载渲染过程结束了。

总结

通过上述过程,我们可以看到项目加载的过程是index.tml->main.js->app.vue->index.js->helloworld.vue。这里只是对我们运行项目后,如何出现首页面做了简单的解释,对具体的实现没有进行分析。

 

  • 293
    点赞
  • 682
    收藏
    觉得还不错? 一键收藏
  • 43
    评论
Vue.js使用HTTPS和WSS的步骤如下: 1. 配置HTTPS: - 生成SSL证书:你可以使用自签名证书或购买证书。生成证书后,你将获得一个公钥和私钥文件。 - 在Vue项目的配置文件,找到`devServer`选项,并添加以下配置: ```javascript devServer: { https: { key: fs.readFileSync('path/to/private.key'), cert: fs.readFileSync('path/to/certificate.crt'), }, }, ``` 将`path/to/private.key`和`path/to/certificate.crt`替换为你的私钥和证书文件的路径。 2. 配置WSS: - 在Vue项目,找到用于建立WebSocket连接的代码。 - 将`ws://`替换为`wss://`,以使用加密的WebSocket连接。 下面是一个示例,演示了如何在Vue项目使用HTTPS和WSS: ```javascript // main.js import Vue from 'vue' import App from './App.vue' import router from './router' Vue.config.productionTip = false new Vue({ router, render: h => h(App) }).$mount('#app') ``` ```html <!-- App.vue --> <template> <div id="app"> <h1>Hello Vue!</h1> </div> </template> <script> export default { mounted() { // 使用WSS建立WebSocket连接 const socket = new WebSocket('wss://example.com/socket') socket.onopen = () => { console.log('WebSocket连接已建立') } socket.onmessage = (event) => { console.log('收到消息:', event.data) } socket.onclose = () => { console.log('WebSocket连接已关闭') } } } </script> ``` 请注意,以上示例仅演示了如何在Vue项目配置HTTPS和WSS。实际上,你需要根据你的项目结构和需求进行相应的配置和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 43
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值