简体   繁体   中英

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.

<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
  2. Using getElementById goes against the way Vue is supposed to be used

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
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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