• vue学习之组件传值、自定义事件


    1、静态传值

    注意:在template中不要加样式,因为不会起到任何作用他是不会执行的

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Documenttitle>
    8. <script src="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script>
    9. <style>
    10. style>
    11. head>
    12. <body>
    13. <div id="root">
    14. <com-son movies="狐妖小红娘" haha="我是父组件传过来的一句话">com-son>
    15. div>
    16. <template id="myson">
    17. <div>
    18. <h1>我是子组件h1>
    19. <h1>{
    20. {movies}}h1>
    21. <h1>{
    22. {haha}}h1>
    23. div>
    24. template>
    25. <script>
    26. // 创建子组件
    27. let ComSon = {
    28. template: "#myson",
    29. props: ['movies', "haha"],
    30. }
    31. const vm = new Vue({
    32. el: '#root',
    33. data() {
    34. return {
    35. }
    36. },
    37. methods: {
    38. },
    39. // 注册成一个局部组件
    40. components: {
    41. ComSon
    42. }
    43. });
    44. script>
    45. body>
    46. html>

    效果图:

    2、动态传值 

    要保证父组件里面的变量名=props里面的值=templ中的插值{ {}}

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Documenttitle>
    8. <script src="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script>
    9. <style>
    10. .fa {
    11. width: 800px;
    12. height: 800px;
    13. background-color: PINK;
    14. padding: 20px;
    15. }
    16. .fa button {
    17. width: 300px;
    18. height: 100px;
    19. margin-top: 20px;
    20. }
    21. .son {
    22. width: 600px;
    23. height: 600px;
    24. background-color: green;
    25. }
    26. style>
    27. head>
    28. <body>
    29. <div id="root" class="fa">
    30. <com-son :message="msg">com-son>
    31. <button @click="change">改变msg值button>
    32. div>
    33. <template id="myson">
    34. <div class="son">
    35. <h1>我是子组件h1>
    36. <h1>{
    37. { message }}h1>
    38. div>
    39. template>
    40. <script>
    41. let ComSon = {
  • 相关阅读:
    【已解决】Failed to find Ceres - Found Eigen dependency
    IDEA上配置mysql
    Linux进程概念
    Docker Build Cache 缓存清理
    使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据
    小程序如何设置自动预约快递
    1393. 股票的资本损益
    注册阿里云账号,免费领取云服务器!
    万字长文硬核AQS源码分析
    什么蓝牙耳机适合运动、运动用的蓝牙耳机推荐
  • 原文地址:https://blog.csdn.net/ywforever/article/details/127358589