简体   繁体   English

我如何将 vanilla Js 代码转换为 vue js 代码

[英]How do i convert vanilla Js code into vue js code

 //Logged out Events document.getElementById("login-help").addEventListener("click", function(){ document.querySelector(".forgotten").style.display = "flex"; document.querySelector(".login").style.display = "none"; document.querySelector(".signup").style.display = "none"; }) document.getElementById("forgotten-back").addEventListener("click", function(){ document.querySelector(".forgotten").style.display = "none"; document.querySelector(".login").style.display = "flex"; document.querySelector(".signup").style.display = "none"; }) document.getElementById("signup-btn").addEventListener("click", function(){ document.querySelector(".forgotten").style.display = "none"; document.querySelector(".login").style.display = "none"; document.querySelector(".signup").style.display = "flex"; }) document.getElementById("signup-back").addEventListener("click", function(){ document.querySelector(".forgotten").style.display = "none"; document.querySelector(".login").style.display = "flex"; document.querySelector(".signup").style.display = "none"; })

This is my vanilla Js code, I am trying to convert this into vue js code, however when I input this code I get an error.这是我的香草 Js 代码,我正在尝试将其转换为 vue js 代码,但是当我输入此代码时出现错误。 How can I convert my vanilla js code to vue js?如何将我的 vanilla js 代码转换为 vue js?

 <script> // @ is an alias to /src import HelloWorld from "@/components/HelloWorld.vue"; export default { name: "Home", methods: { //Logged out Events document.getElementById("login-help").addEventListener("click", function(){ document.querySelector(".forgotten").style.display = "flex"; document.querySelector(".login").style.display = "none"; document.querySelector(".signup").style.display = "none"; }) document.getElementById("forgotten-back").addEventListener("click", function(){ document.querySelector(".forgotten").style.display = "none"; document.querySelector(".login").style.display = "flex"; document.querySelector(".signup").style.display = "none"; }) document.getElementById("signup-btn").addEventListener("click", function(){ document.querySelector(".forgotten").style.display = "none"; document.querySelector(".login").style.display = "none"; document.querySelector(".signup").style.display = "flex"; }) document.getElementById("signup-back").addEventListener("click", function(){ document.querySelector(".forgotten").style.display = "none"; document.querySelector(".login").style.display = "flex"; document.querySelector(".signup").style.display = "none"; }) }, components: { HelloWorld } }; </script>

You add click events in template section of your vue file, not in script(js) section.您在 vue 文件的模板部分添加点击事件,而不是在脚本(js)部分。 It usually looks like this:它通常看起来像这样:

 <button @click="yourFunction">Click me</button>

and then in your methods you have:然后在你的方法中你有:

 methods: {
   yourFunction() {
     this.addClass = true;
   },
  } 

and there is many ways to add styles, one of them is to bind class:并且有很多方法可以添加样式,其中一种是绑定类:

 <button :class="{'hidden': addClass}"  

and you have someBool variable in data:并且您在数据中有 someBool 变量:

  data() {
    return {
     addClass: false,
    };
  }  

You can read more at official docs: https://v2.vuejs.org/v2/guide/events.html and https://v2.vuejs.org/v2/guide/class-and-style.html您可以在官方文档中阅读更多内容: https ://v2.vuejs.org/v2/guide/events.html 和https://v2.vuejs.org/v2/guide/class-and-style.html

您需要根据要发生的操作绑定类: Vue 绑定类和样式您绑定的类需要由data()定义,然后将事件添加到 html 元素以切换这些类或以任何方式操作

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

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