[英]how do I pass url in vue js methods and trigger on @click
Okay so I am doing laravel vue js project and I want to pass a link in a method (code below).好的,所以我正在做 laravel vue js 项目,我想在方法中传递一个链接(下面的代码)。 on click of the div, I want to be taken to the site ( www.google.com ) as seen below.
单击 div 时,我希望被带到该站点 ( www.google.com ),如下所示。 I have tried some solutions given on similar posts here but no luck.
我在这里尝试了类似帖子中给出的一些解决方案,但没有运气。
<template>
<div style="cursor:pointer;" @mouseover="showMe" @mouseout="hideMe" @click="goToMySite">
<h6 v-show="goTolink === false">Hover Me</h6>
<h6 v-show="goToLink === true"> Click to visit My Site </h6>
</div>
</template>
<script>
export default {
props: [],
data() {
return {
goTolink: false,
}
},
methods: {
showMe(){
this.goTolink = true
},
hideMe(){
this.goTolink = false
},
goToMySite(){
href='http://www.google.com'
}
}
}
</script>
<style>
</style>
Use the location
object of window
.使用
window
的location
对象。
goToMySite(){
window.location.href='http://www.google.com'
}
There are several issues in your code.您的代码中有几个问题。
mouseenter
/ mouseleave
instead of mouseover
/ mouseout
which trigger a lot of unnecessary eventsmouseenter
/ mouseleave
而不是mouseover
/ mouseout
这会触发很多不必要的事件v-if
/ v-else
instead of v-show
in this kind of use case.v-if
/ v-else
而不是v-show
。href
doesn't exists in your goToMySite
methodgoToMySite
方法中不存在href
Here's a working example :这是一个工作示例:
<template>
<div style="cursor:pointer;">
<h6 v-if="! goTolink"
@mouseenter="showMe">Hover Me</h6>
<h6 v-else
@mouseleave="hideMe"
@click="goToMySite">Click to visit My Site</h6>
</div>
</template>
<script>
export default {
props: [],
data() {
return {
goTolink: false,
href: 'https://www.google.com'
}
},
methods: {
showMe() {
this.goTolink = true
},
hideMe() {
this.goTolink = false
},
goToMySite() {
// Do what you want here, for example
window.open(this.href, '_blank');
}
}
}
</script>
And a snippet (window.open doesn't works here but it's ok in a regular file) :还有一个片段(window.open 在这里不起作用,但在常规文件中没问题):
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui"> </head> <body> <div id="app"> <div style="cursor:pointer;"> <h6 v-if="! goTolink" @mouseenter="showMe">Hover Me</h6> <h6 v-else @mouseleave="hideMe" @click="goToMySite">Click to visit My Site</h6> </div> </div> <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script> <script> new Vue({ el: '#app', data: { goTolink: false, href: 'https://www.google.com' }, methods: { showMe() { this.goTolink = true }, hideMe() { this.goTolink = false }, goToMySite() { // Do what you want here, for example console.log(`Let's visit ${this.href}`); window.open(this.href, '_blank'); } } }) </script> </body> </html>
Some tips by the way :顺便提一些提示:
false
, null
and undefined
, you can test if a value doesn't exists using ! value
false
、 null
和undefined
之间的差异,否则您可以使用! value
! value
(and just value
if you want to know if the variable is defined) ! value
(如果您想知道变量是否已定义,则只需value
)a
tag for your link (and style it so it looks like your h6
)a
标签为您的链接(和风格,这样它看起来像你的h6
) You could also handle the goToLink
value change in some other ways (but these are subjective choices).您还可以通过其他方式处理
goToLink
值更改(但这些都是主观选择)。 For example in the template (so you can remove the methods showMe
and hideMe
) :例如在模板中(因此您可以删除
showMe
和hideMe
方法):
<h6 v-if="! goTolink"
@mouseenter="goTolink = true">Hover Me</h6>
<h6 v-else
@mouseleave="hideMe"
@click="goTolink = false">Click to visit My Site</h6>
or或者
<h6 v-if="! goTolink"
@mouseenter="goTolink = ! goTolink">Hover Me</h6>
<h6 v-else
@mouseleave="hideMe"
@click="goTolink = ! goTolink">Click to visit My Site</h6>
or keep only one method for toggling :或仅保留一种切换方法:
<h6 v-if="! goTolink"
@mouseenter="toggleLink">Hover Me</h6>
<h6 v-else
@mouseleave="toggleLink"
@click="toggleLink">Click to visit My Site</h6>
methods: {
toggleLink() {
this.goTolink = ! this.goTolink;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.