• 03 Vue脚手架


    1 安装

    配置npm的源为淘宝源:

     安装命令:

    1. npm install -g @vue/cli
    2. # OR
    3. yarn global add @vue/cli
    4. #检查
    5. vue --version
    6. @vue/cli 5.0.8

    升级

    1. npm update -g @vue/cli
    2. # 或者
    3. yarn global upgrade --latest @vue/cli

    2、创建项目

    1. vue create learn-one
    2. 选择vue2或者3
    3. #完成
    4. 🎉 Successfully created project learn-one.
    5. 👉 Get started with the following commands:
    6. $ cd learn-one
    7. $ npm run serve

    启动项目

    1. #执行
    2. $ cd learn-one
    3. $ npm run serve
    4. #输出
    5. DONE Compiled successfully in 3904ms
    6. App running at:
    7. - Local: http://localhost:8080/
    8. - Network: http://10.91.81.99:8080/
    9. Note that the development build is not optimized.
    10. To create a production build, run npm run build.

    访问:http://localhost:8080/

     脚手架启动OK;

    3、脚手架目录分析

     package.json  npm脚本命令

     package-lock.json 锁定包的依赖

     src下的

    main.js 是运行npm run serve最先开始执行的文件,是vue的一切的开始

    1. /**
    2. * 项目的入口文件
    3. */
    4. //导入vue
    5. import Vue from 'vue'
    6. //导入App组件
    7. import App from './App.vue'
    8. //关闭vue的生产提示
    9. Vue.config.productionTip = false
    10. new Vue({
    11. //将app组件放入容器中
    12. //引入的vue是不能配置模板的,引入vue是精简版本vue.runtime.esm.js,不包含模板解析器
    13. render: h => h(App),
    14. }).$mount('#app')

    app.vue是的app组件,是所有组件的父组件;

    pubilc下的index.html是网页的容器:

    1. html>
    2. <html lang="">
    3. <head>
    4. <meta charset="utf-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width,initial-scale=1.0">
    7. <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    8. <title><%= htmlWebpackPlugin.options.title %>title>
    9. head>
    10. <body>
    11. <noscript>
    12. <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.strong>
    13. noscript>
    14. <div id="app">div>
    15. body>
    16. html>

    分析:main.js 中: render:  h=>h(App)

    改写为熟悉的写法:

    1. new Vue({
    2. el:"#root",
    3. template: ``,
    4. comments: {App}
    5. })

    运行发现不会出现页面:报错

    1. [Vue warn]: You are using the runtime-only build of Vue where
    2. the template compiler is not available. Either pre-compile
    3. the templates into render functions, or use the compiler-included build.

    因为这种方式引入的vue不是完整的vue,不包含模板解析器,所以报错了:

    import Vue from 'vue'
    引入vue是精简版本vue.runtime.esm.js,不包含模板解析器
    

     通过render函数,render默认被vue框架调用,引入的一个完整的vue.js,

    通过render可以创建具体的元素;

    1. import Vue from 'vue'
    2. // import App from './App.vue'
    3. Vue.config.productionTip = false
    4. // new Vue({
    5. // render: h => h(App),
    6. // }).$mount('#app')
    7. new Vue({
    8. el:"#app",
    9. render(createElement) {
    10. console.log('render,创建元素')
    11. return createElement('h1','你好啊')
    12. },
    13. // template: ``,
    14. // comments: {App}
    15. })

    所以 render: h => h(App)  实际上类似:  template: ``,  官方 通过render这种方式可以引入完整版本的vue.js 包含模板解析;为什么呢?

    开发的时候需要模板解析器,模板解析器占用vue的1/3代码, 最后打包的时候不需要vue的解析器,所以官网设计了很多精简版本的js。框架通过了render的方式引入完整vue。

  • 相关阅读:
    Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
    np.nan, np.isnan, None, pd.isnull, pd.isna 整理与小结
    docker-compose
    大型国企用泛微OA,让会务管理有序,让会议开展高效
    多维分析预汇总应该怎样做才管用?
    尼得科电机的强大性能,将列车门和屏蔽门的开合变得从容而安全
    [474]. 一和零
    代码随想录算法公开课!
    Flutter 实现背景 Parallax 动画
    java高校自习室座位预订系统springboot+vue
  • 原文地址:https://blog.csdn.net/LoveG_G/article/details/127678041