将jsColor lib添加到vue组件

时间:2017-08-08 09:56:48

标签: javascript html vue.js components vuejs2

我正在尝试将简单的jsColor lib添加到我的vuejs项目中,我正在使用vueJS中的webpack简单。

所以我想将我的脚本添加到此组件中:

<template>
    <div>
        <h3 class="text-center">Paragraph</h3>
        <div class="form-group">
            <label for="fontSize" class="control-label col-md-2">font-size</label>
            <div class="col-md-2">
                <input v-model="paragraph.fontSize" type="number" min="1" class="form-control">
            </div>
            <div class="col-md-1">
                <img class="changeColor" @click="alignment('left')" width="30" height="30" src="../../assets/left-alignment.png" />
            </div>
            <div class="col-md-1">
                <img class="changeColor" @click="alignment('center')" width="30" height="30" src="../../assets/center-alignment.png" />
            </div>

            <div class="col-md-1">
                <img class="changeColor" @click="alignment('right')" width="30" height="30" src="../../assets/right-alignment.png" />
            </div>
            <div class="col-md-1">
                <img class="changeColor" @click="alignment('justify')" width="30" height="30" src="../../assets/justify.png" />
            </div>
            <div class="col-md-1">
                <span>{{paragraph.align}}</span>
            </div>
        </div>
        <div class="row">
            <div class="col-md-offset-2 col-md-6">
                Color:
                #<input class="jscolor" v-model="paragraph.color">
            </div>
        </div>
        <div class="form-group">
            <label for="paragraph" class="control-label col-md-2">Text</label>
            <div class="col-md-8">
                <textarea v-model="paragraph.text" class="form-control" rows="5" id="comment"></textarea>
            </div>
        </div>
        <div class="col-md-offset-2 col-md-8">
            <button @click.prevent="addParagraph()" class="btn btn-success btn-block">Add Paragraph</button>
        </div>
    </div>
</template>


<script>
import jsColor from '../../static/js/jscolor.js';
export default {
    data() {
        return {
            paragraph: {
                text: "",
                fontSize: 14,
                key: "Paragraph",
                align: "left",
                color: "000000",
                paragraph: {}
            }
        }
    },
    methods: {
        addParagraph() {
            var paragraph = {
                key: this.paragraph.key,
                text: this.paragraph.text,
                fontSize: this.paragraph.fontSize,
                align: this.paragraph.align,
            }
            this.$store.commit("appendToDocument", paragraph)
            var panelObj = {"section":"paragraph","color":"list-group-item-success","action":"added"};
            this.$store.commit("appendToPanel", panelObj);
        },
        alignment(option) {
            this.paragraph.align = option;
        }
    },
}
</script>

<style>
.margin-above {
    margin-top: 40px
}

.changeColor {
    padding: 5px;
}

.changeColor:hover {
    background-color: gray;
}
</style>

我已经尝试添加导入和require,唯一有用的是在vuejs生命周期内使用挂载的函数,但问题是它只反映在第一页加载,如果再次打开组件脚本没有用。

任何帮助?

1 个答案:

答案 0 :(得分:-1)

以下是您可能感兴趣的示例组件:

https://gist.github.com/niveshsaharan/1c1567d8475b46d6886e387953596e9e

这里是演示:https://codesandbox.io/s/6wolq8k4jz

<template>
<div class="input-control color-input">
    <input :value="value"
           :id="id"
           class="jscolor-input {hash:true,styleElement:'',onFineChange:'jsColorOnFineChange(this)'}"
           @change="onChange($event.target)"
           @input="onChange($event.target)"
           @focus="showColorPicker"
           @onFineChange="onFineChange"
           ref="color_input"
           maxlength="7"
    />
    <span class="color-value" ref="color_span" @click="showColorPicker"></span>
</div> 
</template>

<script>
import jscolor from './../plugins/jscolor/jscolor.js';
export default {
    name: 'jscolor',
    data(){
        return {
            color: '#000000'
        }
    },
    props: [
        'value',
        'id'
    ],
    methods: {
        onChange(target){
            this.color = target.jscolor.toHEXString();
            this.$refs.color_span.style.backgroundColor = this.color;
            this.$emit('input', this.color);
        },
        onFineChange(e){
            this.color = '#' + e.detail.jscolor;
            this.$refs.color_span.style.backgroundColor = this.color;
            this.$emit('input', this.color);
        },
        showColorPicker(){
            this.$refs.color_input.jscolor.show();
        }
    },
    mounted: function () {
        window.jscolor.installByClassName('jscolor-input');
        this.$refs.color_span.style.backgroundColor = this.value;
    },
    updated: function () {
        this.$refs.color_span.style.backgroundColor = this.value;
    }
}

window.jsColorOnFineChange = function(thisObj){
    thisObj.valueElement.dispatchEvent(new CustomEvent("onFineChange", {detail: {jscolor: thisObj}}));
}

用法:

    <jscolor id="colorThemeTextColor" v-model="theme_text_color"></jscolor>