How can I utilize the data() function and v-model to enhance my code?

Brand new to vue.js and working on a single-file component page where I need to submit data in the textarea along with checking if the checkbox is selected.

I understand that I have to include something like v-model in the textarea, but I'm struggling with this as the api shown in the image below.

Could someone please guide me on what needs to be done in the data() {return {}} section? I find this part of vue.js quite confusing, and any assistance would be greatly appreciated.

https://i.sstatic.net/PeEx3.png

<template>
  <div>
    <div class="add-show">
      <div>
        <p class="title-comment">comment</p>
        <textarea class="content-comment" placeholder="write sth here" v-model="content"></textarea>
      </div>
       <div class="post-wrap">
        <div></div>
        <div class="post-content">
          <input type="checkbox" class="checkbox">
        </div>
      </div>
      <mt-button type="primary" class="mt-button">submit</mt-button>
  </div>
</template>

<script>
import dataService from 'services/dataService'
export default {
  data () {
    return {
      content: '',
      recommend: false
    }
  },
  methods: {
    addShow: function () {
      dataService.postShow(profile).then(() => {
        this.$toast({
        message: '发布成功',
        position: 'bottom'
        })
      })
    }
  }
}
</script>

<style scoped>
</style>

Answer №1

Check out this sample fiddle I created for you here.

Data in Vue are reactive variables, meaning that changes to these variables within a Vue instance will automatically reflect in the views and vice versa. This is known as two-way binding.

v-model establishes a two-way binding on form input elements or components. To learn more about it, visit the official documentation here and here, which is an excellent resource for mastering Vue.

Code

HTML:

<div id="demo">
    <div class="add-show">
      <div>
        <p class="title-comment">comment</p>
        <textarea class="content-comment" placeholder="write sth here" v-model="content"></textarea>
      </div>
       <div class="post-wrap">
        <div></div>
        <div class="post-content">
          <input type="checkbox" class="checkbox" v-model="recommend">
        </div>
      </div>
      <button type="primary" class="mt-button">submit</button>
      <br>
      content:: {{content}}
      <br>
      recommend :: {{recommend}}
  </div>

JS:

var demo = new Vue({
    el: '#demo',
    data: function(){
        return {
        content: '',
      recommend: false
      };
    }
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the most efficient way to update a field in a MongoDB collection?

My dataset looks like this: {"id":1,"Timestamp":"Mon, 11 May 2015 07:57:46 GMT","brand":"a"} {"id":2,"Timestamp":"Mon, 11 May 2015 08:57:46 GMT","brand":"a"} I'm aiming for the following data format: {"id":1,"Timestamp":ISODate("2015-05-11T07:57: ...

Using JavaScript to open a new window and display CSS while it loads

I am looking to utilize a new window for printing a portion of HTML content. var cssLink = document.getElementByTagName('link')[2]; var prtContent = document.getElementById('print_body'); var WinPrint = window.open(' ...

Excessive task executions by Node schedule

I have successfully created a daily quote generator that sends an email with a new quote every day. The node-schedule package was instrumental in setting this up. I instructed the program to execute a function every day at 16:00: schedule.scheduleJob("* ...

How is it possible that the fourth element in the array displays a length of 24 when all it contains is the word "Spain"?

One challenge I'm facing is comparing the 5th element of my array with the value of the object that has been clicked. Below is the code snippet I am currently using: var options = new Array(); $(".option").click(function compareReaction(name) { ...

The functionality of an external Javascript library fails to operate properly once the website is deployed to a hosting

Encountering a peculiar bug with my website at . The website is supposed to load both the JQuery and EasyUI libraries. JQuery is loading correctly as a function to print out "ready" when the page loads. The issue arises when trying to drag products onto th ...

What is the process for loading my new fonts into an HTML document?

I am trying to incorporate two specific fonts, bigsmalls-bold and lft-etica-web, into my CSS and HTML code. Unfortunately, I have been unable to find a way to download these fonts directly. The only information I found was a JavaScript code snippet on I a ...

`Regular expression for allowing only hyphens and numbers`

I am currently using the RegEx pattern ^[0-9]+$" to only allow digits, but I also want to include hyphens - and spaces as valid characters. Can anyone provide assistance in modifying the RegEx pattern accordingly? Previously, I attempted to achieve this ...

Navigate to a fresh HTML page upon form submission

I'm completely new to the world of web apps and JavaScript, and I'm facing an issue where my form submission doesn't redirect me to a thank you page. Instead, it just leads to a blank page every time. I've tried researching solutions on ...

Combining two different arrays in JavaSscript to create a single array

I have two arrays, one representing parents and the other representing children in a relational manner. I need to combine these into a single array. Parent array: const cat = ['a','b','c']; Child array: const sub =[{name:&ap ...

The jquery animation feature seems to be malfunctioning as the div element is not displaying

My goal is to create a div that smoothly appears and disappears while moving randomly, but I'm having trouble as it seems fixed in place. Below is a snippet of the code causing issues. Currently, the div only fades in and out. Any assistance would be ...

Angular2 Window Opener

Trying to establish communication between a child window and parent window in Angular 2, but I'm stuck on how to utilize window.opener for passing a parameter to Angular 2. In my previous experience with Angular 1.5, I referenced something similar he ...

Retrieving and updating data in IE/Edge through dataTransfer methods

While developing a React Application, I encountered an issue in Internet Explorer and Edge where the following lines of code were causing errors: Error (1) Message: Element not found. Code: onDragStart={event => { event.dataTransfer.setData(&a ...

Vue-powered custom mute/unmute button malfunctions inexplicably when deployed, despite functioning flawlessly during development phase

I'm currently struggling to understand why the mute/unmute button is not functioning as expected. The issue seems to arise when the code is deployed on Netlify, as it works fine on localhost. Essentially, the button mutes and unmutes the video, but th ...

Issue with React and Material UI: The Textfield's "onChange" event is not being triggered

I have been attempting to trigger an onchange function when my Textfield is populated, but for some reason the function never seems to be activated. Despite seeing changes triggered by the React devtool plugin in Chrome, I am at a loss. Any suggestions? i ...

Configuring Braintree Client with JS v3 - encountering a null payment_method_nonce causing issues with form submission

I have successfully integrated Braintree into my Laravel 5.2 app using JS v2 client setup, but now I want to upgrade to v3. I have customized the code from the docs as follows: <form id="checkout-form" action="/checkout" method="post"> <div id= ...

Having trouble with accessing environment variables dynamically in NextJS

I'm encountering an issue where I can't access environment variables dynamically in NextJS. Inside my .env.local file, I have the following entry: NEXT_PUBLIC_TEST=test Within my _app.tsx file, I've written the following code: const test = ...

Facing challenges with using express.static() to display an image from a directory within my server on the front end

My application showcases various items on a specific page. Each item includes an image, number, and name. The images are stored in a folder named "uploads" on the backend. I can view the images within this folder, and from the Node backend, I'm provid ...

When utilizing JavaScript to input text, I have observed that if I enter text in one text box, any previously entered value is automatically deleted

Currently, I am facing an issue with 3 text boxes in a row that I am populating using JavaScript. The problem arises when I enter text into one field and then move to the second box to input text - the value from the first text box gets removed. Below is ...

What is preventing the listener from activating?

I came across some HTML code that looks like this: <form id="robokassa" action="//test.robokassa.ru/Index.aspx" method="post"> <input type="text" id="OutSum" name="OutSum" value="" placeholder="Сумма пополнения"> ...

Is it possible to extract data from a null value when trying to access the 'password' property?

const express = require("express"); const mongoose = require("mongoose"); const bodyparser = require("body-parser"); const path = require("path"); const router = express.Router(); const db = mongoose.connection; con ...