๐Ÿ’ป์›น(Web)

Vue ๋ฌธ๋ฒ• ์ •๋ฆฌ

stonesy 2025. 1. 12. 21:37
728x90

1. v-text

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  </head>
  <body>
    <div id="app1">
      <p>{{myText}}</p>
      <p v-text="myText"></p>
    </div>
    <script>
      new Vue({
        el: "#app1",
        data: {
          myText: "Hello!",
        },
      });
    </script>
  </body>
</html>

2. ๋ฐ์ดํ„ฐ ํƒ€์ž…

 

Vue์—์„œ ๋ฐ์ดํ„ฐํƒ€์ž…์€ ์ˆซ์žํ˜•, ๋ฌธ์žํ˜•, Boolean์ด ์žˆ๋‹ค.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>vue ๋ฌธ์žํ˜• example</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  </head>
  <body>
    <div id="app1">
      <p>{{ myPrice * 1.08 }}</p>
      <p>{{ "์•ˆ๋…•ํ•˜์„ธ์š”~ "+ myName + "๋‹˜" }}</p>
      <p>{{ myName.substr(0,1) }}</p>
    </div>
    <script>
      new Vue({
        el: "#app",
        data: {
          myPrice: 500, //์ˆซ์žํ˜•
          myName: "ํ™๊ธธ๋™", //๋ฌธ์žํ˜•
        },
      });
    </script>
  </body>
</html>

3. v-on

์ด๋ฒคํŠธ ์ง€์ •. ์•ฝ์–ด๋กœ @๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>v-on example</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  </head>
  <body>
    <div id="app1">
      <p v-text="value"></p>
      <button v-on:click="setValue('1๋ฒˆ')">1๋ฒˆ ๋ฒ„ํŠผ</button>
      <button @click="setValue('2๋ฒˆ')">2๋ฒˆ ๋ฒ„ํŠผ</button>
    </div>
    <script>
      new Vue({
        el: "#app1",
        data: {
          value: "ํด๋ฆญํ•ด ๋ณด์„ธ์š”.",
        },
        methods: {
          setValue(str) {
            this.value = str + " ํด๋ฆญ";
          },
        },
      });
    </script>
  </body>
</html>

4. v-bind

์†์„ฑ์— vue ์ธ์Šคํ„ด์Šค์˜ ์†์„ฑ์ด๋‚˜ ๋ฉ”์†Œ๋“œ, JavaScript ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>v-bind example</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  </head>
  <body>
    <div id="app1">
      <a v-bind:href="url">๋„ค์ด๋ฒ„</a>
      <a :href="getUrl('google')">๊ตฌ๊ธ€</a>
    </div>
    <script>
      const app = new Vue({
        el: "#app1",
        data: { url: "https://www.naver.com" },
        methods: {
          getUrl: function (name) {
            return "https://" + name + ".com";
          },
        },
      });
    </script>
  </body>
</html>

5. v-model

์–‘๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <title>v-model example</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  </head>
  <body>
    <div id="app1">
      <!--v-model์„ ์‚ฌ์šฉํ•ด ํ”„๋กœํผํ‹ฐ์— ๊ฐ’์„ input๊ฐ’์œผ๋กœ ์ €์žฅ๊ฐ€๋Šฅ-->
      <input type="text" v-model="input_model" placeholder="์ด๋ฆ„" />
      <p>input model: {{input_model}}</p>

      <!--lazy๋Š” input์˜ ํฌ์ปค์Šค๋ฅผ ๋‹ค๋ฅธ๊ณณ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ํ”„๋กœํผํ‹ฐ์— ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ-->
      <input type="text" v-model.lazy="input_model2" />
      <p>input lazy model : {{input_model2}}</p>

      <!--textarea์—์„œ๋„ v-model ์‚ฌ์šฉ๊ฐ€๋Šฅ-->
      <textarea v-model="textarea_model"></textarea>
      <p>textarea model : {{textarea_model}}</p>

      <!--checkbox์—์„œ ์‚ฌ์šฉ ์˜ˆ์ œ ์ €์žฅ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ v-bind๋ฅผ ํ†ตํ•ด ๋กœ์ง ์‹คํ–‰๊ฐ€๋Šฅ-->
      <input type="checkbox" v-model="checkbox_model" value="awd" />
      <button v-bind:disabled="checkbox_model === false">disabled</button>
      <p>checkbox model : {{checkbox_model}}</p>

      <!--ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ฐฐ์—ด๋กœ ์„ ์–ธํ•˜๋ฉด value๊ฐ’์„ ์ €์žฅ-->
      <input type="checkbox" v-model="checkbox_model2" value="test1" />
      <input type="checkbox" v-model="checkbox_model2" value="test2" />
      <p>checkbox model : {{checkbox_model2}}</p>

      <!--๋ผ๋””์˜ค ํ”„๋กœํผํ‹ฐ ์ €์žฅ-->
      <input type="radio" v-model="radio_model" value="test1" />
      <input type="radio" v-model="radio_model" value="test2" />
      <input type="radio" v-model="radio_model" value="test3" />
      <p>radio model : {{radio_model}}</p>

      <!--select ํ”„๋กœํผํ‹ฐ ์ €์žฅ-->
      <select v-model="select_model">
        <option>test1</option>
        <option>test2</option>
      </select>
      <p>select model : {{select_model}}</p>
    </div>
    <script>
      const app = new Vue({
        el: "#app1",
        data: {
          input_model: "",
          input_model2: "",
          textarea_model: "",
          checkbox_model: false,
          checkbox_model2: [],
          radio_model: "",
          select_model: "",
        },
      });
    </script>
  </body>
</html>

6. v-if

์กฐ๊ฑด๋ฌธ

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>v-if example</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  </head>
  <body>
    <div id="app">
      <!--๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํด๋ฆญํ•œ text์ถœ๋ ฅ๊ณผ ํด๋ฆญํ•œ ๋ฒ„ํŠผ ๋น„ํ™œ์„ฑ-->
      <button v-bind:disabled="one" v-on:click="oneCheck">one</button>
      <button v-bind:disabled="two" v-on:click="twoCheck">two</button>
      <button v-bind:disabled="three" v-on:click="threeCheck">three</button>
      <p v-if="one">onc click</p>
      <p v-else-if="two">two click</p>
      <p v-else>three click</p>
    </div>

    <script>
      new Vue({
        el: "#app",
        data: {
          one: false,
          two: false,
          three: true,
        },
        methods: {
          oneCheck() {
            this.one = true;
            this.two = false;
            this.three = false;
          },
          twoCheck() {
            this.one = false;
            this.two = true;
            this.three = false;
          },
          threeCheck() {
            this.one = false;
            this.two = false;
            this.three = true;
          },
        },
      });
    </script>
  </body>
</html>

7. v-for๋ฐ˜๋ณต๋ฌธ

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>v-for example</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  </head>
  <body>
    <div id="app">
      <ul>
        <!--arr๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ item์— ๋‹ด๋Š”๋‹ค-->
        <li v-for="(item, index) in arr" :key="index">{{ item }}</li>
        <br />

        <!--object์˜ key๊ฐ’๊ณผ value๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์ถœ๋ ฅ๊ฐ€๋Šฅ-->
        <li v-for="(value, key) in obj" :key="key">{{ key }} : {{ value }}</li>
        <br />

        <!--objectArray์˜ item(Object)๋ฅผ ์ถœ๋ ฅ-->
        <li v-for="(item, index) in objArr" :key="index">
          {{ item.a}} : {{ item.b }} : {{ item.c }}
        </li>
        <br />

        <!--์ž…๋ ฅ๊ฐ’์„ ์ถ”๊ฐ€ ํ•˜๋Š” ๊ธฐ๋Šฅ-->
        <input v-model="num" />
        <button v-on:click="add(num)">add</button>
        <button v-on:click="deletes()">delete</button>
        <li v-for="item in pushArr">{{item}}</li>
      </ul>
    </div>

    <script>
      new Vue({
        el: "#app",
        data: {
          arr: ["์จˆ๋นต", "๋ฉœ๋ก ๋นต", "ํฌ๋กœ์™€์‚ฌ"],
          obj: { a: 1, b: 2, c: 3 },
          objArr: [
            { a: 1, b: 2, c: 3 },
            { a: 4, b: 5, c: 6 },
          ],
          pushArr: [],
          num: 0,
        },
        methods: {
          add(val) {
            this.pushArr.push(val);
          },
          deletes() {
            this.pushArr.pop();
          },
        },
      });
    </script>
  </body>
</html>

 

 

 

https://cjw-awdsd.tistory.com/33

 

[Vue] Vue.js ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ์ด์ •๋ฆฌ

์ตœ๊ทผ ํ”„๋กœ์ ํŠธ์—์„œ ํ”„๋ก ํŠธ์—”๋“œ Jquery, Ajax๋ฅผ ์ ๊ทน ํ™œ์šฉํ•ด๋ณด๋ฉฐ ํ”„๋ก ํŠธ์— ์•ฝ๊ฐ„ ๊ด€์‹ฌ์ด ์ƒ๊ฒจ Vue๋ฅผ ํ•œ๋ฒˆ ๊ณต๋ถ€ํ•ด๋ดค๋‹ค. Vue์— ๋Œ€ํ•œ ๊ฐœ๋…์€ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋ฌธ๋ฒ•๋งŒ ๋”ฑ ์ •๋ฆฌํ•˜๊ณ ์ž ํ•œ๋‹ค. 1. Vue ์ธ์Šคํ„ด์Šค Vue.js๋กœ

cjw-awdsd.tistory.com

 

728x90

'๐Ÿ’ป์›น(Web)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง  (1) 2024.12.20
Spring  (0) 2024.12.02
JSP(Jakarta Server Pages), JSTL(JSP Standard Tag Library)  (0) 2024.12.01
Servlet  (0) 2024.12.01
[๊ณตํ†ตํ”„๋กœ์ ํŠธ]5์ฃผ์ฐจ: React Openvidu ๊ตฌํ˜„  (0) 2024.08.11