什么叫emit选项

news/2024/7/10 2:50:44 标签: vue

emits 选项

为什么要验证事件
验证抛出的事件,啥又叫抛出?

pp.component('custom-form', {
  emits: {
    // 没有验证
    click: null,

    // 验证submit 事件
    submit: ({ email, password }) => {
      if (email && password) {
        return true
      } else {
        console.warn('Invalid submit event payload!')
        return false
      }
    }
  },
  methods: {
    submitForm(email, password) {
      this.$emit('submit', { email, password })
    }
  }
})

就是验证邮箱账号,密码吗??你倒是说说是这个场景啊,用一句话说一下啊。

以下代码是空白

<!DOCTYPE html>
<div>
  <my-component v-model:title="bookTitle"></my-component>
</div>
</html>

<script src="https://unpkg.com/vue@next"></script>
<script>
app.component('my-component', {
  props: {
    title: String
  },
  emits: ['update:title'],
  template: `
    <input
      type="text"
      :value="title"
      @input="$emit('update:title', $event.target.value)">
  `
}).mount('#bookTitle')
</script>

可以改吗,应该怎么改,对吗?
也不对

<!DOCTYPE html>
<div>
  <my-component v-model:title="bookTitle"></my-component>
</div>
</html>

<script src="https://unpkg.com/vue@next"></script>
<script>
app.component('my-component', {
  props: {
    title: String
  },
  emits: ['update:title'],
  template: `
    <input
      type="text"
      :value="title"
      @input="$emit('update:title', $event.target.value)">
  `
})
Vue.createApp(app).mount('#bookTitle')
</script>

终于有一个可以试运行的了。看了半天都没有,组件本来就不好懂,还没有例子试。

有点乱七八糟的。

<!DOCTYPE html>
<user-name
  v-model:first-name="firstName"
  v-model:last-name="lastName">
</user-name>
</html>

<script src="https://unpkg.com/vue@next"></script>
<script>
app.component('user-name', {
  props: {
    firstName: String,
    lastName: String
  },
  emits: ['update:firstName', 'update:lastName'],
  template: `
    <input
      type="text"
      :value="firstName"
      @input="$emit('update:firstName', $event.target.value)">
  
      <input
      type="text"
      :value="lastName"
      @input="$emit('update:lastName', $event.target.value)">
  `
};

const HelloVueApp = {
  components: {
    UserName,
  },
  data() {
    return {
      firstName: 'John',
      lastName: 'Doe',
    };
  },
};

Vue.createApp(HelloVueApp).mount('#model-example')
</script>

运行不出来。

运行出来了

<!DOCTYPE html>
<div id ="v-model-example">
  <p>First name: {{ firstName }}</p>
  <p>Last name: {{ lastName }}</p>
<user-name
  v-model:first-name="firstName"
  v-model:last-name="lastName">
</user-name>
</div>
</html>

<script src="https://unpkg.com/vue@next"></script>
<script>
//app.component('user-name', {
  const UserName = {
  props: {
    firstName: String,
    lastName: String
  },
  //emits: ['update:firstName', 'update:lastName'],
  template: `
    <input
      type="text"
      :value="firstName"
      @input="$emit('update:firstName', $event.target.value)">
  
      <input
      type="text"
      :value="lastName"
      @input="$emit('update:lastName', $event.target.value)">
  `
};

const HelloVueApp = {
  components: {
    UserName,
  },
  data() {
    return {
      firstName: 'John',
      lastName: 'Doe',
    };
  },
};

Vue.createApp(HelloVueApp).mount('#v-model-example')
</script>

抄一个
第一个字母自动大写了。

<!DOCTYPE html>
<div id="app">
  <my-component v-model.capitalize="myText"></my-component>
  {{ myText }}
</div>
</html>

<script src="https://unpkg.com/vue@next"></script>
<script>
const app = Vue.createApp({
  data() {
    return {
      myText: ''
    }
  }
})

app.component('my-component', {
  props: {
    modelValue: String,
    modelModifiers: {
      default: () => ({})
    }
  },
  emits: ['update:modelValue'],
  methods: {
    emitValue(e) {
      let value = e.target.value
      if (this.modelModifiers.capitalize) {
        value = value.charAt(0).toUpperCase() + value.slice(1)
      }
      this.$emit('update:modelValue', value)
    }
  },
  template: `<input
    type="text"
    :value="modelValue"
    @input="emitValue">`
})

app.mount('#app')
</script>

http://www.niftyadmin.cn/n/957527.html

相关文章

RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 ‘mat2‘

问题&#xff1a; RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 ‘mat2’ 异常代码行&#xff1a; prediction net.forward(b_x) 解决方法&#xff1a; 在代码前添加一行&#xff0c;将输入的数据转成是dtypetorch.floa…

post并不是内置,是随着实例(例子)的可变而可变的。不是出现了这么多次,它就是关键词,就是内置的。props是关键词,是一些属性,可变的,动态的。可字符串,可对象

post: {id: 1,title: My Journey with Vue }props的例子 以字符串数组形式列出的 prop&#xff1a;props: [title, likes, isPublished, commentIds, author]但是&#xff0c;通常你希望每个 prop 都有指定的值类型。 这时&#xff0c;你可以以对象形式列出 prop&#xff0c;这…

pytorch tensor与numpy转换

tensor to numpy a torch.ones(5) print(a) 输出 tensor([1., 1., 1., 1., 1.]) 进行转换 b a.numpy() print(b) 输出 [1. 1. 1. 1. 1.] 注意&#xff0c;转换后的tensor与numpy指向同一地址&#xff0c;所以&#xff0c;对一方的值改变另一方也随之改变 a.add_(1) p…

<slot name=“girl“></slot>了也没显示出该显示内容啊

<!DOCTYPE html> <div id"app"><child-component><template slot"girl">漂亮、美丽、购物、逛街</template><template slot"boy">帅气、才实</template><div>我是一类人&#xff0c;我是默认的…

ConvLSTM的用法

简单RNN与LSTM对比 LSTM计算示意 LSTM计算示意 import torch from torch import nn import torch.nn.functional as f from torch.autograd import Variable# Define some constants KERNEL_SIZE 3 PADDING KERNEL_SIZE // 2class ConvLSTMCell(nn.Module):""&quo…

这个命令错了没有。npm init vitejs/app guagua2

两个父组件子组件&#xff0c;放在一个component文件夹里&#xff0c;是不是不行&#xff0c;还需要用到通信工具&#xff1f; 所以需要建立打包的工具&#xff1f; 又换了命令了。 npm init vite <project-name> -- --template vue

PyTorch的torch.cat

1. 字面理解&#xff1a;torch.cat是将两个张量&#xff08;tensor&#xff09;拼接在一起&#xff0c;cat是concatnate的意思&#xff0c;即拼接&#xff0c;联系在一起。 2. 例子理解 >>> import torch >>> Atorch.ones(2,3) #2x3的张量&#xff08;矩阵…

组件第一个功能:App.vue是父组件,是包在外面的组件,在components文件夹里的组件,HelloWorld.vue是子组件,props是父组件给子组件传值

msg的值在父组件里&#xff0c;在app.vue里 给子组件helloworld.vue里的msg传值。 好好体会一下。 emits又是怎样使用呢&#xff1f; 哪些情况下&#xff0c;什么情况下&#xff0c;子组件需要给父组件传值呢&#xff1f; 父组件 <template><div id"app"…