Laravel 6.4.1 + webpack + vue + scss动态加载

时间:2019-12-11 10:16:38

标签: vue.js webpack laravel-6

我有laravel 6.4.1,对默认的webpack配置进行了较小的更改。 我也在使用具有范围样式的vue组件。

运行npm run dev时,一切正常。我的Vue组件已加载并具有样式。 运行npm run production时,未加载我的Vue组件。 还是很好...已加载JS文件,但是该组件从未触发createdmounted并且在屏幕上不可见,在DOM中也不可见。

我怎么知道它当时已加载?

当我将console.log('test')放在export default上方(或下方)时,它将显示在控制台中。

当我完全移除<style scoped lang="scss">标签时,我的组件也在屏幕上可见。

我已经尝试删除部分样式,但是它永远无法正常工作。即使是空样式标签也不会呈现组件。只有将其完全删除后,它才能工作。

当然,我想保留组件中的样式,那么如何解决此问题?

我已经从Vue组件中删除了一些JS,以使其更具可读性,并且由于我强烈怀疑该问题不在JS中,因此我认为该问题没有任何价值。

webpack.mix.js

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix
    .js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css');

if (mix.inProduction()) {
    mix.version();
} else {
    mix.sourceMaps();
}

// webpack.mix.js
const path = require('path'),
    WebpackShellPlugin = require('webpack-shell-plugin'),
    BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin,
    {CleanWebpackPlugin} = require('clean-webpack-plugin');

mix.webpackConfig({
    plugins: [
        new WebpackShellPlugin({
            onBuildStart: [
                'php artisan js-localization:export --quiet',
                'php artisan ziggy:generate resources/js/ziggy-routes.js --quiet'
            ]
        }),
        new BundleAnalyzerPlugin({
            analyzerMode: mix.inProduction() ? 'disabled' : 'server',
            openAnalyzer: false
        }),
        new CleanWebpackPlugin({
            dry: false,
            cleanStaleWebpackAssets: true,
            cleanOnceBeforeBuildPatterns: [],
            cleanAfterEveryBuildPatterns: [
                'js/chunk/*'
            ],
            dangerouslyAllowCleanPatternsOutsideProject: true,
        }),
    ],
    resolve: {
        alias: {
            ziggy: path.resolve('vendor/tightenco/ziggy/dist/js/route.js'),
        },
    },
    output: {
        publicPath: '/',
        chunkFilename: 'js/chunk/[name].[chunkhash].js',
    },
});

Vue组件

<template>
    <div class="position-relative my-2" :style="{backgroundColor: properties.settings.backgroundColor}">
        <block-template-content-text
            v-if="canDisplayBlock(block, 'content', 'text')"
            :text-config="tinyMce.text"
            :block="block"
            @block-change="storeBlockChange">
        </block-template-content-text>
        <block-template-content-text-image
            v-if="canDisplayBlock(block, 'content', 'text-image')"
            :text-config="tinyMce.text"
            :image-config="tinyMce.image"
            :block="block"
            @block-change="storeBlockChange">
        </block-template-content-text-image>

        <div class="d-flex align-items-center justify-content-center flex-column position-absolute controls">
            <i class="fas fa-2x fa-fw fa-chevron-up cursor-pointer" @click="$emit('sort-item', 'up', block)"></i>
            <div>
                <i class="fas fa-1x fa-fw fa-plus-circle cursor-pointer" @click="$emit('add-new-block', block)"></i>
                <!-- delete section start -->
                <i class="fas fa-1x fa-fw fa-trash cursor-pointer"
                   :id="'delete-' + block.hash"
                   @click="$emit('delete-block', $event, block)"></i>
                <b-tooltip :target="'delete-' + block.hash"
                           :id="'tooltip-' + block.hash"
                           triggers="focus">
                    <button type="button"
                            class="btn btn-primary"
                            @click="$emit('cancel-delete-block', block)">
                        {{ Lang.get('general.buttons.cancel') }}
                    </button>

                    <button type="button"
                            class="btn btn-danger"
                            @click="$emit('delete-block', $event, block)">
                        <i class="fas fa-fw fa-trash"></i>
                        {{ Lang.get('general.buttons.delete')}}
                    </button>
                </b-tooltip>
                <!-- delete section end -->

                <!-- popover start -->
                <div class="popover-content" :hidden="!showPopover">
                    <i class="fas fa-fw fa-times close-popover cursor-pointer" @click="closePopover"></i>
                    <div class="form-group">
                        <label for="background-color">
                            {{ Lang.get('project.design.pages.block.settings.background-color') }}
                        </label>
                        <input type="color"
                               name="background-color"
                               id="background-color"
                               class="form-control"
                               v-model="properties.settings.backgroundColor"
                               value="#ffffff"
                               @input="setSetting('backgroundColor', $event)">
                    </div>
                </div>
                <i class="fas fa-1x fa-fw fa-cog cursor-pointer"
                   @click="togglePopover"></i>
                <!-- popover end -->
            </div>
            <i class="fas fa-2x fa-fw fa-chevron-down sort-order cursor-pointer"
               @click="$emit('sort-item', 'down', block)"></i>
        </div>
    </div>
</template>

<script>
    // Bootstrap Vue
    import {TooltipPlugin} from 'bootstrap-vue';
    Vue.use(TooltipPlugin);

    // TinyMCE editor
    import 'tinymce/tinymce.min';
    import 'tinymce/themes/silver/theme.min';
    import 'tinymce/plugins/paste';
    import 'tinymce/plugins/link';
    import 'tinymce/plugins/imagetools';
    import {EventBus} from "../../vue/EventBus";

    export default {
        name: "display-block",
        components: {
            'block-template-content-text': () => import('./blocks/content/Text'),
            'block-template-content-text-image': () => import('./blocks/content/TextImage'),
        },
        props: {
            block: {
                required: true,
                type: Object
            }
        },
        data() {
           [...]
        },
        methods: {
            [...]
        },
        mounted() {
            [...]
        }
    }
</script>

<style scoped lang="scss">
    @import "../../../sass/variables";
    @import "~bootstrap/scss/mixins";
    @import "~bootstrap/scss/bootstrap-grid";
    @import "~bootstrap/scss/utilities/position";
    @import "~bootstrap/scss/popover";

    // Bootstrap Vue
    @import '~bootstrap-vue/src/index.scss';

    .block {
        .block- {
            &text {
                @import '~tinymce/skins/ui/oxide/skin.min.css';
                @import '~tinymce/skins/ui/oxide/content.min.css';
                @import '~tinymce/skins/content/default/content.min.css';
            }
        }
    }

    .controls {
        @extend .position-relative;

        top: map_get($sizes, 50);
        right: - map_get($spacers, 5);
        transform: translateY(-50%);

        &:first-child {
            @extend .d-none;
        }

        .popover-content {
            @extend .popover;
            @extend .p-2;

            min-width: 150px;
            min-height: 150px;

            .close-popover {
                @extend .position-absolute;
                @extend .mt-2;
                @extend .mr-2;

                top: 0;
                right: 0;
            }

            input {
                &[type=color] {
                    @extend .p-0;

                    width: 25px;
                    height: 25px;
                    border: none;
                }
            }
        }
    }
</style>

page.js

import {EventBus} from "../../vue/EventBus";
import {TooltipPlugin} from 'bootstrap-vue';
// import DisplayBlock from "../../components/project/DisplayBlock";

if (document.getElementById('page-editor')) {
    // Bootstrap Vue
    Vue.use(TooltipPlugin);

    new Vue({
        el: '#page-editor',
        components: {
            BlockModal: () => import('../../components/project/BlockModal'),
            DisplayBlock: () => import('../../components/project/DisplayBlock'),
            // DisplayBlock
        },
        data: {
            [...]
        },
        computed: {
            [...]
        },
        watch: {
            [...]
        },
        methods: {
            [...]
        },
        mounted() {
            [...]
        }
    });
}

1 个答案:

答案 0 :(得分:0)

在您的webpack文件中,缺少VUE的解析器。

  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js' // 'vue/dist/vue.common.js' 
    }
  }

尝试添加以上代码段。