如图所示,此为父组件中的数组数据
如图,我想实现的是 点击下方某一个搜索引擎时,搜索框左边的图标就会相应的转换成那个搜索引擎的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>
父组件向子组件传递text值:
父组件:<searchBar :text="text"/>
子组件接收:props: ["text"]
--
要在search中定义一个text,当click时修改这个text值。
然后在<searchBar :text="text"/>,传递给子组件,子组件就能接收到这个text值了