easycom自动导入自定义组件

目录下 components / MyItem /MyItem.vue

<template>
<view>
<view class="item">
(。・∀・)ノ゙嗨
</view>
</view>
</template>
<style lang="scss">
.item{
width: 200rpx;
height:200rpx;
background-color: orange;
}
</style>

使用

<template>
<view>
<MyItem></MyItem>
<!-- 其他东西 -->
</view>
</template>

props父向子组件传值

父传子

<!-- 组件 Components/MyItem.vue-->
<view class="pubTitle">
<view class="big">
{{title}}
</view>
<view class="small">
{{subtitle}}
</view>
<view class="line">
</view>
</view>
</template>
<script>
export default {
props:["title","subtitle"],
data() {
return {
};
}
}
</script>
<style lang="scss">
.pubTitle{
padding: 60rpx 30rpx;
text-align: center;
.big{
font-size: 50rpx;
font-weight: 600;
color: #333;
}
.small{
font-size: 28rpx;
color: #888;
}
.line{
display: inline-block;
width: 80rpx;
height: 8rpx;
background-color: #1AA034;
border-radius: 10rpx;
}
}
</style>

使用组件方

 <MyItem title="首页" subtitle="index Page"></MyItem>
<!-- 驼峰写法也好使 -->
<My-item title="首页" subtitle="index Page"></My-item>

绑定动态值及数据类型默认值

动态传值的使用组件方

<template>
<view>
<MyItem :title="text" :subtitle="subtitle"></MyItem>
</view>
</template>
<script>
export default {
data(){
return{
text:"(。・∀・)ノ゙嗨",
subtitle:"┗|`O′|┛ 嗷~~"
};
}
}
</script>

数据默认值的组件方

<template>
<view class="pubTitle">
<view class="time">
{{time}}
</view>
<view class="big">
{{title}}
</view>
<view class="small">
{{subtitle}}
</view>
<view class="list">
{{list}}
</view>
<view class="obj">
{{obj}}
</view>
<view class="line">
</view>
</view>
</template> <script>
export default {
// props:["title","subtitle"],
props:{
title:{
type: String,
default:"默认标题"
},
subtitle:{
type: String,
default:"默认副标题"
},
// 父组件传值时。除string都要v-bind
time:{
type: Number,
default:Date.now()
},
// 数组和对象都得 default() return
list:{
type:Array,
default(){
return [1,2,3]
}
},
obj:{
type:Object,
default(){
return {
a:1,
b:2
}
}
}
},
data() {
return { };
}
}
</script>

emit子向父组件传值

子组件

<template>
<view>
<view class="" >
<input type="text" placeholder="请输入"
@input="inputsomeThing"
>
</view>
</view>
</template> <script>
export default {
methods:{
inputsomeThing(e){
// this.$emit(事件名,值)
// 只传单值
// this.$emit("myenv", {
// e.detail.value,
// ) // 传对象(多值)
this.$emit("myenv", {
value:e.detail.value,
time:Date.now()
})
}
}
}
</script>

父组件

<template>
<view class="box">
<myEvent @myenv="myenv"></myEvent>
</view>
</template>
<script>
export default {
data(){
return{
};
},
methods:{
myenv(e){
console.log(e)
}
}
}
</script>

native 修饰符与父子间通信传值

父组件

<template>
<view class="box">
<myEvent
@myenv="myenv"
@click.native="onCLick"
></myEvent>
<!--
就是你写 @click,它会以为我们自定义了个事件
所以后面加个native,说明这是原生的,不是在父子组件通信的
-->
<button @click="turnON">开启</button>
<!-- 核心还是通过props传数据给子组件,只是结合了使用修饰符让原生事件在子组件标签上生效而已-->
<!-- 数据向下走,方法向上走 -->
<mypop :state="state"
@fade="fade"
></mypop>
</view>
</template>
<!-- <box-icon name='home-alt-2' flip='horizontal' color='#a09898' ></box-icon> -->
<script>
export default {
data(){
return{
state:false
};
},
methods:{
myenv(e){
console.log(e)
},
onCLick(){
console.log("(。・∀・)ノ゙嗨")
},
turnON(){
this.state = true
},
fade(e){
this.state = e
}
}
}
</script>

子组件 myEvent

<template>
<view>
<view class="" >
<input type="text" placeholder="请输入"
@input="inputsomeThing"
>
</view>
</view>
</template>
<script>
export default {
data() {
return { };
},
methods:{
// e.detail.value
inputsomeThing(e){
// 事件名,使用时前面加on
// 只传单值
// this.$emit("myenv", {
// e.detail.value,
// ) // 传多值
this.$emit("myenv", {
value:e.detail.value,
time:Date.now()
})
}
}
}
</script>

子组件 mypop

<template>
<view>
<view class="pop"
:style="{display: state? 'block':'none'}"
>
</view>
<button size="mini" @click="fade">关闭</button>
<view class="">
(。・∀・)ノ゙嗨
</view>
</view>
</template>
<script>
export default {
name:"mypop",
props:{
state:{
type:Boolean,
default:false
}
},
data() {
return {
};
},
methods:{
fade(){
this.$emit("fade",false)
}
}
}
</script>
<style lang="scss">
.pop{
height: 300rpx;
width: 300rpx;
background-color: cornflowerblue;
}
</style>

sync修饰符以及update的实现原理

两种方法对比

不使用sync

父组件

<template>
<view class="box">
<button @click="turnON">开启</button>
<mypop :state="mystate"
@update:state = "aaa"
></mypop>
</view>
</template>
<script>
export default {
data(){
return{
mystate:false
};
},
methods:{
turnON(){
this.mystate = true
},
aaa(e){
this.mystate = e
}
}
}
</script>

子组件

<template>
<view>
<view class="pop"
:style="{display: state? 'block':'none'}"
>
</view>
<button size="mini" @click="fade">关闭</button>
<view class="">
(。・∀・)ノ゙嗨
</view>
</view>
</template> <script>
export default {
name:"mypop",
props:{
state:{
type:Boolean,
default:false
}
},
data() {
return {
};
},
methods:{
fade(){
this.$emit("update:state",false)
}
}
}
</script> <style lang="scss">
.pop{
height: 300rpx;
width: 300rpx;
background-color: cornflowerblue;
}
</style>

使用sync

父组件

<template>
<view class="box">
<button @click="turnON">开启</button>
<!-- 使用.sync可以修改父子局传来的值但无法触发自定义事件 -->
<!-- 如果选的时vue3的话,父组件需要写成“v-model:state” -->
<mypop :state.sync="mystate" ></mypop>
</view>
</template>
<!-- <box-icon name='home-alt-2' flip='horizontal' color='#a09898' ></box-icon> -->
<script>
export default {
data(){
return{
mystate:false
};
},
methods:{
turnON(){
this.mystate = true
}
}
}
</script>

子组件

<template>
<view>
<view class="pop"
:style="{display: state? 'block':'none'}"
>
</view>
<button size="mini" @click="fade">关闭</button>
<view class="">
(。・∀・)ノ゙嗨
</view>
</view>
</template>
<script>
export default {
name:"mypop",
props:{
state:{
type:Boolean,
default:false
}
},
data() {
return {
};
},
methods:{
fade(){
this.$emit("update:state",false)
}
}
}
</script>
<style lang="scss">
.pop{
height: 300rpx;
width: 300rpx;
background-color: cornflowerblue;
}
</style>

vue中的生命周期和小程序周期的对比

VUE的生命周期 https://www.jianshu.com/p/4f8daeafe58f



普通vue页面

<template>
<view class="box">
</view>
</template>
<script>
export default {
data(){
return{ };
},
methods:{
interval(){
console.log("自定义函数")
}
},
created(){
this.interval()
//网络请求
console.log("这是 index created")
}, mounted(){
//数据渲染
console.log("index mounted")
}
}
</script>

APP.vue页面

也可以使用 mounted ,created 都会自动转化。但既然它本身使用了onLaunch这些东西,就用就行,不用往上添加了。

<script>
export default {
onLaunch: function() {
console.log('App Launch')
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
}
}
</script> <style>
/*每个页面公共css */
</style>

uniapp学习(二)的更多相关文章

  1. uni-app学习(二)

    1. uni-app学习(二) 1.1. 好用css记录 一定透明度的背景色background: rgba(255,255,255,.6); 1.2. 好用的代码段 store(用户登录) expo ...

  2. uni-app学习(五)好用的插件3

    1. uni-app学习(五)好用的插件3 1.1. 分享推广页面 分享推广页面,分享第三方.保存二维码.复制推广地址 模板地址 示例 这个用到的几率还是蛮大的,可以直接拿来修改下用 1.2. 教育A ...

  3. emberjs学习二(ember-data和localstorage_adapter)

    emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...

  4. ReactJS入门学习二

    ReactJS入门学习二 阅读目录 React的背景和基本原理 理解React.render() 什么是JSX? 为什么要使用JSX? JSX的语法 如何在JSX中如何使用事件 如何在JSX中如何使用 ...

  5. TweenMax动画库学习(二)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  6. Hbase深入学习(二) 安装hbase

    Hbase深入学习(二) 安装hbase This guidedescribes setup of a standalone hbase instance that uses the local fi ...

  7. Struts2框架学习(二) Action

    Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...

  8. Python学习二:词典基础详解

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...

  9. Quartz学习--二 Hello Quartz! 和源码分析

    Quartz学习--二  Hello Quartz! 和源码分析 三.  Hello Quartz! 我会跟着 第一章 6.2 的图来 进行同步代码编写 简单入门示例: 创建一个新的java普通工程 ...

  10. SpringCloud学习(二):微服务入门实战项目搭建

    一.开始使用Spring Cloud实战微服务 1.SpringCloud是什么? 云计算的解决方案?不是 SpringCloud是一个在SpringBoot的基础上构建的一个快速构建分布式系统的工具 ...

随机推荐

  1. ROS机器人校正

    vROS机器人IMU自动校正 连接小车 注意:必须在同一区域网 ssh clbrobort@clbrobort 激活树莓派主板 roslaunch clbrobot bringup.launch 自动 ...

  2. extend笔记

    JavaScript面向对象 继承extend 1. 概念(主要用途) 将子类中的共性代码 ( 属性和方法 ) 抽取出来 放到父类中 每当有一个新的子类需要用到共性的属性或者方法时 不需要在自己内容复 ...

  3. RTP分包模式(H264/H265)

    在rfc6184-h264文档5.4章节有详细说明.以及rfc7798-h265文档4.2章节中也有部分介绍. 一.NALU Header ● H264 NALU Header(1 byte)结构图如 ...

  4. 《HelloGitHub》第 85 期

    兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...

  5. YOLO1论文中文版

    文章目录 YOLO1中文版 摘要 1. 引言 2. 统一检测 2.1 网络设计 2.2 训练 2.3 推断 2.4 YOLO的限制 3. 与其它检测系统的比较 4. 实验 4. 1 与其它实时系统的比 ...

  6. MASA MinimalAPI源码解析:为什么我们只写了一个app.MapGet,却生成了三个接口

    源码解析:为什么我们只写了一个app.MapGet,却生成了三个接口 1.ServiceBase 1.AutoMapRoute 源码如下: AutoMapRoute自动创建map路由,MinimalA ...

  7. Prism Sample 23-RegionMemberLifetime

    在导航中跳转时,视图是缓存的.如果要求某视图在离开后就销毁,需要实现 public class ViewAViewModel : BindableBase, INavigationAware, IRe ...

  8. Pwn系列之Protostar靶场 Stack1题解

    (gdb) disasse main Dump of assembler code for function main: 0x08048464 <main+0>: push ebp 0x0 ...

  9. 2020-11-07:已知一个正整数数组,两个数相加等于N并且一定存在,如何找到两个数相乘最小的两个数?

    福哥答案2020-11-07: 1.哈希法.2.排序+双指针夹逼. golang代码如下: package main import ( "fmt" "sort" ...

  10. golang版本sdl2显示窗体

    golang版本sdl2显示窗体 go用syscall调用sdl2,在win10 x64上没问题,其他系统不敢保证. 见地址 package main import ( "fmt" ...