Home >Web Front-end >JS Tutorial >What are the precautions for using Vue.js?
This time I will bring you what are the precautions for using Vue.js, what are the precautions for using Vue.js, the following is a practical case, let's take a look.
1. When passing parameters, there must be a space between the second parameter and the preceding comma
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(items))
2. Pay attention to the space
Correct format
<script>import Store from './store'console.log(Store)export default { ... }</script> 错误格式 <script> import Store from './store' console.log(Store)export default { ... }</script>
3. The parent passes parameters to the child component
In the parent component
//模板中<template> <div id="app"> //之前老版本 <conponent-a msgfromfather="父亲传给儿子!"></conponent-a> <ConponentA msgfromfather="父亲传给儿子!"></ConponentA> </div></template>//Js<script>export default { //注册ConponentA components: {ConponentA}, }</script>
In the child component
<template> <div class="hello"> <h1>{{ msg }}</h1> <button v-on:click="onClickMe()">点我啊,小样儿</button> </div></template><script> export default { data () { return { msg: 'hello from component A!' } }, //props 可以是数组或对象,用于接收来自父组件的数据 props: ['msgfromfather'], methods: { onClickMe: function () { //打印从父组件传过来的值 console.log(this.msgfromfather) } } }</script><style scoped> h1 { font-weight: normal; }</style>
4. The child passes parameters to the parent component
The son tells The father needs to use vm.$emit and vm.$on to trigger events and listen for events
In child components
<template> <div class="hello"> <h1>{{ msg }}</h1> <h1>{{msgfromfather}}</h1> <button v-on:click="onClickMe()">点我啊,小样儿</button> </div></template><script> export default { data () { return { msg: 'hello from component A!' } }, methods: { onClickMe: function () {// 子传父 触发当前实例上的事件 this.$emit('child-tell-me-something', this.msg) } } }</script><style scoped> h1 { font-weight: normal; }</style>
In parent components
<template> <div id="app"> <p>child tells me: {{childWorlds}}</p> <ConponentA msgfromfather="父亲传给儿子!" v-on:child-tell-me-something="listenToMyBoy"></ConponentA> </div></template><script>import ConponentA from './components/componentA.vue'export default { data: function () { return { childWorlds: '' } }, components: {ConponentA}, watch: { items: { handler: function (items) { Store.save(items) }, deep: true } }, methods: { //监听 listenToMyBoy: function (msg) { console.log(msg) this.childWorlds = msg } } }</script>
In addition to this method, there are For other methods, please see the Vue.js official website
Self-defining componentSpecify attributesData type
export default { props: { slides: { type: Array, //数组 default: [] //默认值 } }, 在加载完毕执行某个方法 mounted () { this.loadxxx() }@mouseover="xxxx" Mouse enter ( Execute an event), @mouseout="xxxx" The mouse moves out (execute an event);Transiotions animation is invalid for left and right, etc. To achieve animation effects, you can only use the x-axis;slot slotthis.abc = false is equivalent to this['abc'] = falseThe parent component style does not add scoped, so that its child components can share it The style, that is to say, the style of the child component can be written in the parent component.
<!-- Add "scoped" attribute to limit CSS to this component only --><style>......</style>& represents the parent element
<!--css书写规范 可被继承的写在前面,不让继承的的写在后面--><style lang="stylus" rel="stylesheet/stylus"> #app .tab display: flex width: 100% height: 40px line-height: 40px .tab-item flex: 1 text-align: center /* & > a & 代表父元素 tab-item 子元素选择器 */ & > a display: block font-style: 14px color: rgb(77,85,93) &.active color: rgb(240,20,20)</style>1 pixel border implementation
can be used on the PC Through the following settings, it is achieved.
border-bottom: 1px solid rgba(7,17,27,0.1)I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! Recommended reading:
Deep into the movement of JS in JavaScript
Deep into the advanced application of DOM in JavaScript
In-depth knowledge of JavaScript
The above is the detailed content of What are the precautions for using Vue.js?. For more information, please follow other related articles on the PHP Chinese website!