본문 바로가기
study/Vue.js

Vue.js 인라인 바인딩 inline binding

by 고기만두(개발자) 2022. 6. 11. 23:49
728x90
반응형
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>
    <script>
        window.onload = function(){
            var vm1 = new Vue({
                el : '#test1',
                data : {
                    bgcolorValue : 'red',
                    colorValue : 'yellow',
                    styleValue : {
                        backgroundColor : 'red',
                        color : 'yellow'
                    }
                },
                methods : {
                    setInlineStyle : function(){
                        this.bgcolorValue = 'yellow',
                        this.colorValue = 'red'
                    },
                    setStyleValue : function(){
                        this.styleValue = {
                            backgroundColor: 'yellow',
                            color : 'red'
                        }
                    }
                }
            })
        }
    </script>
</head>
<body>
    <div id='test1'>
        <h3 style = 'background-color:red; color:yellow'>문자열</h3>
        <h3 v-bind:style='{backgroundColor:bgcolorValue, color:colorValue}'>문자열</h3>
        <button type='button' v-on:click='setInlineStyle'>inline css 변경</button>

        <h3 v-bind:style='styleValue'>문자열</h3> <!--여러개 동시에 셋팅할때는 data에서 묶어다 객체로 불러와도 됨-->
        <button type='button' v-on:click='setStyleValue'>객체 변경</button>
    </div>
</body>
</html>

스타일 지정할 때 css로 처리하는 방법은 여기

css없이도 처리할 수 있을까 그러면?

 

정답! 있다

 

1. style = ' ' <- 따옴표 안에 일일이 인라인으로 지정해버리는 방법

2. 뷰 객체의 data에 스타일 속성을 지정한 후  v-bind:style = {} 안에서 지정하는 방법

배경 / 글씨색 등 개별의 속성도 지정할 수 있고, 이를 객체로 묶어서 한번에 지정할 수 있는 방법도 있음.

vue.js인라인 바인딩inline binding

결과적으로 두 버튼의 결과는 같다

728x90
반응형

댓글