• webpack +vue开发(2)


    我们的loader方式其实可以写成inline的方式

        loaders:[
                {
                    test:/.js$/,
                    loader:"babel",
                    exclude:/node_modules/,
                }
            ]

    直接在entry中写上

    require("!style!css!../css/style.css");

    推荐直接使用loader的方法,下面使用vue写一个小例子,首先安装

    npm install vue vue-loader vue-html-loader vue-style-loader vue-template-compiler --save-dev

    接下来写我们的loader

    module.exports = {
        devtool:"sourcemap",
        entry:"./js/entry.js",
        output:{
            filename:"bundle.js",
        },
        module:{
            loaders:[
                {
                    test:/.css$/,
                    loader:"style!css"
                },
                {
                    test:/.js$/,
                    loader:"babel",
                    exclude:/node_modules/,
                },
                {
                    test:/.vue$/,
                    loader:"vue"
                }
            ]
        },
        babel:{
            presets:['es2015','stage-0'],
            plugins:['transform-runtime']
        }
    }

     配置好之后我们现在js下创建一个 components放我们的组件,然后在components下创建一个heading.vue,(最简单的vue组件)

    <template>
        <div>
            <h1>{{message}}</h1>
        </div>
    </template>
    <script type="text/javascript">
        export default{
            data(){
                return {
                    message:"hello vue"
                }
            }
        }
    </script>

    然后我们在我们的入口文件引入我们vue组件和vue.js并且实例化vue

    require("./module-one.js");
    require("./module-two.js");
    
    import Vue from 'vue';
    import Heading from './components/heading.vue';
    
    new Vue({
        el:'#app',
        components:{Heading}
    });
    
    require("../css/style.css");

    然后再去我们的index.html中配置

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <div id="app">
            <Heading></Heading>        
        </div>
        <h1>webpck is nice tool</h1>
        <script type="text/javascript" src="bundle.js"></script>
    </body>
    </html>

    这里的Heading就是entry.js import的Heading和components的Heading应该是一致的。然后运行webpack之后会出现如下错误

    这是由于npm安装vue不是常规模式,要使用常规模式可以通过script标签引入或者添加一个配置

    module.exports = {
        devtool:"sourcemap",
        entry:"./js/entry.js",
        output:{
            filename:"bundle.js",
        },
        module:{
            loaders:[
                {
                    test:/.css$/,
                    loader:"style!css"
                },
                {
                    test:/.js$/,
                    loader:"babel",
                    exclude:/node_modules/,
                },
                {
                    test:/.vue$/,
                    loader:"vue"
                }
            ]
        },
        resolve:{
            alias:{
                'vue$':"vue/dist/vue.js"
            }
        },
        babel:{
            presets:['es2015','stage-0'],
            plugins:['transform-runtime']
        }
    }

     这样你就可以看到hello vue显示在页面了,还有另外一种方式全局性的components注册

    require("./module-one.js");
    require("./module-two.js");
    
    import Vue from 'vue';
    Vue.component('Heading',require('./components/heading.vue'));
    
    new Vue({
        el:'#app',
    });
    
    require("../css/style.css");
  • 相关阅读:
    11-基于CPCI的中频功率放大收发板
    10-基于TMS320C6678+XC7K325T的6U CPCI Full Camera Link图像处理平台
    141-FMC141-4路 250Msps/16bits ADC, FMC板卡
    125-FMC125-两路125Msps AD,两路160Msps DA FMC子卡模块
    164-基于TI DSP TMS320C6455和Altera FPGA EP2S130的Full CameraLink PDS150接口板卡
    北京太速科技有限公司 layout 事业部
    20-基于 DSP TMS320C6455的6U CPCI高速信号处理板卡
    64-基于TMS320C6455、XC5VSX95T 的6U CPCI无线通信处理平台
    18-基于双TMS320C6678 DSP的3U VPX的信号处理平台
    202-基于TI DSP TMS320C6678、Xilinx K7 FPGA XC72K325T的高速数据处理核心板
  • 原文地址:https://www.cnblogs.com/longsiyuan/p/6092487.html
Copyright © 2020-2023  润新知