在webpack中使用vue的一些注意事项

一、在webpack中安装vue

npm install vue -S   //在运行时需要用到的,也就是生产环境

二、使用vue

  1. 在main.js中引入vue

    // 引入vue
    import Vue from "vue";
    
    const app = new Vue({
      el: "#content",
      data() {
        return {
          message: "我在main.js中通过模块化的方式导入了vue"
        }
      },
      methods: {
        getMsg() { 
          console.log(this.message);
        }
      }
    })
    
  2. 在index.html中设置挂载点

    <div id="content">
        <h3 v-text="messge"></h3>
        <p>{{getMsg()}}</p>
    </div>
    
  3. 执行webpack打包命名后,运行测试

    报错

    You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
    
    image-20200903160237768

    原因: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TXlz0yE7-1599121821562)(F:\markdown笔记\图片\image-20200903160411703.png)]

    解决办法: 在webpack.config.js配置文件中加入

    resolve: {
        // alias 别名
        alias: {
          "vue$": "vue/dist/vue.esm.js"  //设置新的解析文件
        }
      }
    

三、注意

在index.html中.应该以下面的方式写

<body>
  <div id="app">
    <h3 v-text="message"></h3>
    <p>{{getMsg()}}</p>
  </div>
  <script src="./dist/bundle.js"></script>  //函数导入应该写在vue挂载点的后面
</body>

如果将写在head中,会报如下错误

vue.esm.js:629 [Vue warn]: Cannot find element: #app
vue.esm.js:629 [Vue warn]: Cannot find element: #app

原因:js在dom文档加载之前就执行了vue数据渲染,此时的dom结构还未加载,所以vue会找不到挂载点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值