javascript - 是否可以绑定(bind)在 VueJS 中使用 url() 的 SVG 填充属性?

标签 javascript html css vue.js svg

我需要动态绑定(bind) SVG 矩形元素的填充属性,但它不起作用。这是我的简单 VueJS 组件,用于演示我正在尝试做什么(也可在 codepen 中找到)

<template>
<div>
  <!-- this works but id and fill attributes are hardcoded -->
  <svg class="green" width="100" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <linearGradient id="gradient1">
      <stop stop-color="red" offset="0%" />
      <stop stop-color="blue" offset="100%" />
    </linearGradient>
    <rect width="100" height="50" fill="url(#gradient1)" />
  </svg>

  <!-- this doesn't work... -->
  <svg class="green" width="100" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <linearGradient :id="myid">
      <stop stop-color="red" offset="0%" />
      <stop stop-color="blue" offset="100%" />
    </linearGradient>

    <rect width="100" height="50" :fill="myfill" />
  </svg>
</div>
</template>

<script>
new Vue({
  el: 'body',
  data: {
    title: 'Vuejs SVG binding example',
    myid: 'gradient2',
    myfill: 'url(#gradient2)'
  },
});
</script>

请注意,fill 属性使用 url() 将元素 id 作为参数,这使事情变得复杂。据我所知,fill 属性使用同一组件中定义的 linearGradient 的唯一方法是通过元素 id 属性引用它。

我尝试这样做的原因是因为我想避免在组件内部对 id 进行硬编码。因为我将在网页上有很多这个组件的实例,所以会有多个元素具有相同的 id 值,这是不应该发生的。

最佳答案

是的,可以做想做的事。我做了一些类似的事情,但只有一个 div 而不是 svg。

理论

将动态 css 类名绑定(bind)到 svg 并将填充放入该类中。此链接显示如何将 css 用于 css CSS - Style svg fill with class name

我提出的解决方案假设通过 props 将一些值传递给组件

解决方案

<template>
...

    <rect width="100" height="50" :class="myfill" />
...
</template>

<script>
new Vue({
  el: 'body',
  data: {
    title: 'Vuejs SVG binding example',
    myid: 'gradient2',
    myfill: somePropYouPassedIn
  },
});
</script>

修改第一个答案

在试用您的 fiddle 时,我认为您的编码是正确的,您只是使用了旧版本的 Vuejs(我在尝试让您的 fiddle 工作时注意到这一点)。无论如何,我无法让你的笔与 vue 一起工作,所以我在这里创建了一个全新的 fiddle https://jsfiddle.net/nkalait/ohmzxb7L/

代码

<div>
  {{ title }}
  <!-- this works but id and fill attributes are hardcoded -->
  <svg class="green" width="100" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <rect width="100" height="50" :fill="gradient1" />
  </svg>

  <!-- this doesn't work... -->
  <svg class="green" width="100" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <rect width="100" height="50" :fill="gradient2" />
  </svg>


  // I HAVE PUT THE GRADIENTS IN THERE OWN SVG
  <svg aria-hidden="true" focusable="false" style="width:0;height:0;position:absolute;">
    <linearGradient id="gradient1">
      <stop stop-color="yellow" offset="0%" />
      <stop stop-color="blue" offset="100%" />
    </linearGradient>
    <linearGradient id="gradient2">
        <stop stop-color="red" offset="0%" />
        <stop stop-color="green" offset="100%" />
    </linearGradient>
  </svg>
</div>

关于javascript - 是否可以绑定(bind)在 VueJS 中使用 url() 的 SVG 填充属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58934558/

相关文章:

javascript - 使用 React JS 上传多个文件

javascript - JS 延迟加载图像 - 将测试函数应用于多个 HTML 元素

html - 如何使用 px 回退使 Compass Vertical Rhythm 输出 Rems 而不是 Ems?

Javascript:使用 css 选项卡隐藏/显示内容

css - 禁用 div 内容错误

html - <pre> 标签不适用于 bootstrap 4 模态

javascript - 检查没有模运算符的奇数

javascript - 合并嵌套数组并对值求和

javascript - 在 JavaScript 中操作视频时间戳

javascript - 如何动画 svg 组元素(基本上缩放)