Vue - Snippets: Unterschied zwischen den Versionen
Aus Wikizone
| Zeile 4: | Zeile 4: | ||
* Outputting Data mit | * Outputting Data mit | ||
** Interpolation {{}} | ** Interpolation {{}} | ||
| − | ** Bindings | + | ** Bindings v-bind:property="myVal" |
** Methods | ** Methods | ||
** JavaScript Objects | ** JavaScript Objects | ||
* this | * this | ||
| − | |||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| Zeile 50: | Zeile 49: | ||
</div> | </div> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
| + | == Events == | ||
| + | === v-on === | ||
Version vom 16. Dezember 2020, 23:40 Uhr
Basics
- Data Option / Function
- Methods Option / Object
- Outputting Data mit
- Interpolation {{}}
- Bindings v-bind:property="myVal"
- Methods
- JavaScript Objects
- this
// create App
const app = Vue.createApp();
// mount a html region
// app.mount('cssSelector');
app.mount('#myId'); // Vue controls now this id in the DOM
const app = Vue.createApp({
// DATA FUNCTION can hold key val pairs
data() { //or data: function(){...}
return{ // data always returns an object
myVar: 'Learn Vue',// can store keys with vals of every type(bool, object, string...)
myVar2: 'Master Vue<,
myHTML: '<h3>HTML Code</h3>', // use v-html to output html code
myLink: 'https://viewjs.org'
};
}
// METHODS OBJECT HOLDS FUNCTIONS
methods: {
outputGoal(){
const randomNumber = Math.random();
if (randomNumber < 0.5) {return 'Learn Vue';}
else {return this.myVar2} // 'this' works because vue merges all data and methods in a global vue object
}
}
});
<div id="myId">
<h3>Interpolation</h3>
<p>{{ myVar }}</p> <!-- Interpolation outputs "Learn Vue" -->
<h3>Binding</h3>
<p>Use bindings to set attributes. I.e. set the href attribute. {{myLink}} wouldn't work inside of tags.</p>
<p>Learn more <a v-bind:href="myLink">about Vue</a></p>
<p>{{ outputGoal() }}</p><!-- functions or simple js expresseions like 1+1 work to -->
<p v-html="myHTML"></p>
</div>