一最安 2021-10-05 21:09 采纳率: 84.4%
浏览 55
已结题

请问父组件里的数组中的某条数据如何传递给子组件?

img

img

img


如图,我想实现的是 点击下方某一个搜索引擎时,搜索框左边的图标就会相应的转换成那个搜索引擎的logo

我原想着传search组件数组中的text数据到子组件searchBar来,但是报错了

请问该如何处理呢?


<template>
  <div class="search-bar">
    {{text}}
    <div class="select-box">
      <span class="search-logo"></span>

      <i id="down" class="el-icon-caret-bottom" @click="putList"></i>
    </div>

    <input type="text"  v-model="inputText" @change="toSearch(inputText)"
           placeholder="输入并搜索" />
  </div>
</template>
<script>
export default {
  data() {
    return {
      inputText:''
    }
  },
  props:['text'],
  methods: {
    putList(){
      var downlogo=document.getElementsById("down")
      console.log(downlogo)
      downlogo.className="el-icon-caret-up"
      console.log(document.getElementsByClassName('el-icon-caret-up'))

    },

    toSearch(text){
      console.log("输入搜索框的内容:",text)
      this.$emit("inputTxt",text)
    }
  },
}
</script>
<style  scoped>
.search-bar {
  position: absolute;
  width: 459px;
  height: 64px;
  line-height: 64px;
  left: 490px;
  top: 157px;
  background: #ffffff;
  border: 1px solid #000000;
  box-sizing: border-box;
  box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
  border-radius: 50px;
}
.select-box {
  display: inline-block;
  margin-left: 15px;
  width: 58px;
  height: 47px;
  cursor: pointer;
}
.search-logo{
  display: inline-block;
  transform: translateY(7px);
  width: 30px;
  height: 30px;
  background-image: url('../assets/baidu.png');
  background-repeat: no-repeat;
  background-size: 100%;
}
.el-icon-caret-bottom{
  margin-bottom: 4px;
  cursor: pointer;
}
.search-bar input{
  outline-style: none;
  height:30px;
  border: 0;
  font-size: 24px;
}
</style>


上为searchbar组件,即搜索框
下为search组件,即放置搜索引擎的地方

<template>
  <div class="wrapper-search">
    <searchBar @inputTxt="getInputText"/>

    <div id="search">

      <div id="webList">
        <ul>

          <li class="webList_item"
              v-for="(item,index) in webList"
              :key="index"
              :text="item.txt"
              @click="itemClick(item)">
            <div class="el-icon-circle-check" :style="{float:'right',color:'blue'}"></div>
            <img :src="item.img_src" class="webList_Icon" title="百度" alt="百度"/>

            <p class="webName">{{ item.txt }}</p>

          </li>

        </ul>

      </div>



    </div>
  </div>
</template>

<script>
import searchBar from "@/components/searchBar"
export default {
  name: "search",
  components:{
    searchBar
  },
  data() {
    return {
      dialogVisible: false,
      searchText:'',
      webList:[{
        txt: '百度',
        img_src: require('../assets/baidu.png'),
        url: 'https://www.baidu.com/s?wd=',
        index: 1
      },
        {
          txt: '谷歌',
          img_src: require('../assets/google.png'),
          url: 'https://www.google.com/search?q=',
          index: 2
        },
        {
          txt: '掘金',
          img_src: require('../assets/juejin.png'),
          url: 'https://juejin.cn/search?query=',
          index: 3
        },
        {
          txt: 'github',
          img_src: require('../assets/github.png'),
          url: 'https://github.com/search?q=',
          index: 4
        },

      ]

    }
  },
  methods: {
    itemClick(item){
      console.log("click传过来的item.txt:",item.txt)
      console.log("url:",item.url)
      console.log("this.searchText:",this.searchText)
      var finalUrl=item.url+this.searchText
      console.log("finalUrl:",finalUrl)
      document.location.href=finalUrl;

    },
    getInputText(msg){
      console.log("msg:",msg)
      this.searchText=msg
    },

    putList(){
      var downIcon=document.getElementsByClassName('el-icon-caret-bottom')
      console.log(downIcon)
      downIcon[0].style=('transform:rotate(180deg);transition: 0.4s')
      this.dialogVisible = true
    }
  }



}
</script>




<style>
.wrapper-search {
  width: 100%;
  /*height: 64px;*/
  padding: 100px 0 70px;
}
.webName{
  margin: 0px;
  padding: 0px;
  text-align: center;
  font-size: 14px;

}

div#webList{
  width: 500px;
  background: aliceblue;
  border-radius: 25px;
  border: 2px solid #d3dce6;
  padding:10px;
  margin:15px;
  cursor: pointer;
}

li.webList_item{

  padding: 10px;
  width: 80px;
  height: 80px;

}

li.webList_item:hover{
  font-weight: 500;
  background-color: lightblue;
  color: deepskyblue;
}

div#prepend{
  display: flex;

}

div.imgIcon{
  padding:3px 6px;
  padding-bottom: 0px;
  /*margin: 5px;*/
}

img.imageIcon{
  width: 30px;
  height: 30px;
}

ul{
  border-radius: 2px;
  display: flex;
}


ul,li{
  list-style: none;

  padding: 0px;
  margin: 0px;
}


#search{
  margin:120px;
  margin-left: 450px;
  margin-top: 150px;
}


img.webList_Icon{
  width: 65%;
  height: 65%;
  margin-left: 12px;
  margin-bottom: 5px;
  margin-top: 2px;
}

</style>






  • 写回答

2条回答 默认 最新

  • 本堃不方 2021-10-05 22:15
    关注

    好久不用vue了

        <searchBar @inputTxt="getInputText"/>
    

    父传子,父组件里面,你注册子组件标签里绑定要传递的数据,例如

    父组件

    <searchBar  :toSon="dialogVisible"/>  //把dialogVisible 传递给searchBar  组件,  dialogVisible是data里面的数据
    

    子组件

    props:['toSon']  //接收数据,{toSon}能直接在页面上显示
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月14日
  • 已采纳回答 10月6日
  • 创建了问题 10月5日

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持