简体   繁体   English

如何在VUE Component的Scoped区域使用JSON数据

[英]How to use JSON data in Scoped area of VUE Component

I want to use my JSON data in the Scoped area of VUE Component.我想在 VUE 组件的 Scoped 区域中使用我的 JSON 数据。

So basically my Json file contains CSS styles as text and HTML tags.所以基本上我的 Json 文件包含 CSS 样式作为文本和 HTML 标签。

I got the HTML value with v-html in Vue Component.我在 Vue 组件中使用 v-html 获得了 HTML 值。

but now I want to access the CSS value from JSON File in CSS Scoped area of my VUE Component.但现在我想从 VUE 组件的 CSS 作用域区域中的 JSON 文件访问 CSS 值。

MY JSON我的 JSON

{
    "elements":[
           {
             "id":"11",
             "html":"<button>this is button</button>",
             "css":"button{color:#f00;}",
             "author":"Test"
           }
    ]
}

====================== ======================

MY VUE Component我的 VUE 组件

<template>
<div class="collection">
    <section class="section section-elements">
        <div class="container container--large">
            <div class="row">
                <div class="col-sm-3" v-for="(item, index) in json.elements" v-bind:key="index">
                    <div class="elements">

                        <div class="elements-head" v-html="item.html" :style="item.css">

                        </div>
                        <div class="elements-main">
                            <p>{{ item.css }}</p>
                        </div>
                        <div class="elements-foot">
                            <p>{{ item.author }}</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

It's not possible to dynamically create scoped css styles as these are made by vue-router during the build and not in the client.动态创建作用域 css 样式是不可能的,因为这些样式是在构建期间由 vue-router 制作的,而不是在客户端中制作的。

However your current code should work if you want to apply the style to the <div> element, just include the styling within the curly brackets:但是,如果您想将样式应用于<div>元素,则您当前的代码应该可以工作,只需将样式包含在大括号中:

{
   "id":"11",
   "html":"<button>this is button</button>",
   "style":"font-size:18vw;color:#f00;",
   "author":"Test"
}

<div class="elements-head" v-html="item.html" :style="item.style">

example: https://jsfiddle.net/ellisdod/q3L5ahke/示例: https : //jsfiddle.net/ellisdod/q3L5ahke/

Dynamic Components动态组件

However, If you want to apply styles to the element it's better to use dynamic components.但是,如果您想对元素应用样式,最好使用动态组件。 The drawback to this is that you need to register all of your html scripts as components - you could write a loop to do this programmatically from your json.这样做的缺点是您需要将所有 html 脚本注册为组件 - 您可以编写一个循环来从 json 以编程方式执行此操作。

Vue.component('MyButton',{
template:'<button>this is button</button>'
})
{
    "id":"11",
    "component":"MyButton",
    "style":"font-size:18vw;color:#f00;",
    "author":"Test"
}
<component :is="item.component" :style="item.style">

example: https://jsfiddle.net/ellisdod/oju1m7q8/示例: https : //jsfiddle.net/ellisdod/oju1m7q8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM