简体   繁体   English

在Vue.js中将属性设置为div

[英]Setting Attribute to a div in Vue.js

I've got a Vue file and I would like to set an 'action' attribute of my form. 我有一个Vue文件,我想设置表单的'action'属性。

<template>
  <div>
    <form>
    </form>
  </div>
</template>

export default {
  created() {
    var test = document.getElementById("form");
    test.setAttribute('action', 'file.php');
  }

But setting it in a lifecycle hook doesn't work. 但是将其设置在生命周期挂钩中是行不通的。 What should I do? 我该怎么办?

Couple of things: 几件事情:

  1. The mounted lifecycle hook is usually where you want to perform the initial actions for the component mounted生命周期挂钩通常是您要执行组件初始操作的位置
  2. Using getElementById goes against the way Vue is supposed to be used 使用getElementById违反了应该使用Vue的方式

Something like this would make more sense: 这样的事情会更有意义:

<template>
  <div>
    <form :action='action'>
    </form>
  </div>
</template>

export default {
  data() {
    return {
      action: 'file.php'
    }
  },
  mounted() {
    // initialize things here
  }
}

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

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