What is the best way to invoke a function in a separate scope in Vue.js?

I'm looking to implement a search feature on my website using Vue.js 2 and Laravel. However, I am facing an issue as the search form is not located in the same place as the Vue template.

header.blade.php

<form action="javascript:void(0);" method="get">   
   <input type="search"  placeholder="Search..."  v-model="search" @keyup.enter="searchPost()" autofocus>
 </form>

Home.vue

<div class="panel panel-default" v-for="post in posts">
  <div class="panel-heading">
    <a :href="'/' + $route.params.trans + '/post/' + post.slug"><h3>{{ post.title }}</h3></a>
    <small class="date" v-if="timeLang =='en'">{{ post.created_at | formatDateEn }}</small>
    <small class="date" v-else-if="timeLang =='id'">{{ post.created_at | formatDateId }}</small>
  </div>
  <div class="panel-body">
    <img :src="'/images/post/' + post.post.image" class="img-single-page img-responsive" alt="" v-if="post.post.image">
    <p>{{post.body | strip_tags | trunCate}}</p>
    <a :href="'/' + $route.params.trans + '/post/' + post.slug" class="btn readmore">Read More</a>
  </div>
</div>
<script>
export default {

data()
{
    return {
        posts: {},
    }
},
created(){
  this.$on('fetchdata', this.fetchPost);
},
methods: {
    fetchPost() {
       axios.get('/' + this.$route.params.trans + '/search?search=' + this.search)
     .then(({data}) => {
     this.posts = data
   }
}

app.js

const app = new Vue({
 el: '#app',
 router,
 data: {
     show: false,
     search: '',
 },
 methods: {
 searchPost() {
     this.$emit('fetchdata');
 }
}
});

Answer №1

To incorporate the component, it needs to be defined in the resources/assets/js/app.js file by using the following code:

Vue.component(
  'search-feature', 
  require('./components/search-feature.vue')
);

After defining the component, you can easily utilize it in your views like this:

<search-feature></search-feature>

To test the functionality, you must compile the code. This can be done by running npm run dev or using npm run watch for continuous monitoring of file changes.

Keep in mind that this is a simple illustration and can be customized as required.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for using multiple Angular directive modules in PprodWant to enhance your Pprod experience by

Currently, I am working on jhipster Release 0.7.0 and our jhipster app has multiple types of directive modules – one for the index page and another for common directives. However, when we run the app on Prod profile, an exception occurs: [31mPhantomJ ...

Chrome is having trouble setting the cookie with the Set-Cookie header

I am making an AJAX call to another service's API, expecting to receive a cookie that will be stored in my browser for future API calls. Unfortunately, even though the response headers contain a 'Set-Cookie' header, the cookie is not being s ...

What advantages does using a normalized username in Asp.Net Core offer for users seeking faster search results?

My investigation revealed that out of 100,000 users in Asp.Net, searching with NormalizedUserName is quicker compared to searching with UserName. await _context.Users.Where(u => u.NormalizedUserName == username).FirstOrDefaultAsync(); await _context.U ...

Using Fabric.js to manage image controls situated beneath another overlay image

I'm currently working on a canvas user interface using jquery and the fabric.js library. I managed to add an overlay png image with transparency by implementing the code below: var bgImgSrc = bgImg.attr('src'); canvas.setOverlayImage(bgImgS ...

Utilizing Angular Filter to compare the date in a JSON file with the current system date

<p id="appt_time" ng-if="x.dateTime | date: MM/dd/yyyy == {{todaysDate}}">DISPLAY IF TRUE{{todaysDate}} </p> Plunkr:https://plnkr.co/edit/r2qtcU3vaYIq04c5TVKG?p=preview x.dateTime | date: MM/dd/yyyy retrieves a date and time which results in ...

Storing a selected value in the database using Laravel Blade

Attempting to save information from a select field to the database. Tested this method: <div class="form-group"> <label>Type</label> <input style="width:10%" type="text" name="type" class="form-control" value="{{$findBook-& ...

Having issues with Element UI sorting values in a table

I've been attempting to create a basic sorting function, but it doesn't seem to be functioning correctly. var Main = { data() { return { tableData: [{ value: '1.799,37', name: 'Tom 3' }, { ...

Storing Checkbox Selections in a Database using Ember.js

I am facing an issue with a checkbox that is supposed to save to the database immediately after the value changes. The checkbox is linked to the data element called 'audit'. Below is how the checkbox is implemented in the template: {{view Site.R ...

What are the most effective methods for utilizing React child components?

I have a particular interest in how to efficiently pass information along. In a different discussion, I learned about the methods of passing specific props to child components and the potential pitfalls of using <MyComponent children={...} />. I am ...

Disable video playback on Internet Explorer 7 (with Rob W's method)

I've successfully implemented this script created by Rob W from StackOverflow, but unfortunately it's not working on IE7. You can view the script on this jsfiddle page. I've also tried importing this to enable JSON on IE, but the issue pers ...

Discover the most concise string within the array

Is there a way to determine the shortest string in a JavaScript array regardless of the number of elements it contains? I attempted using var min = Math.min(arr[0].length,arr[1].length,arr[2].length); to find the shortest string among 3 elements in an ar ...

How can I stop labels from overlapping on a victory pie chart?

Currently, I am working on creating a pie chart using Victory Charts in react. However, I have encountered an issue where the labels of my pie chart overlap due to the slices being too small. Here is the documentation for reference: . Additionally, you can ...

I'm seeking an easy method to adjust the x and y coordinates of a popup rectangle with a fixed size. Can anyone provide

Is there a way to create a simple pop-up rectangle, possibly using jQuery or a similar tool, that presents a scaled-down canvas view of a larger browser window (1000px x 1600px), allowing users to click and determine the x/y position within the full window ...

AngularJS - smooth scrolling with $anchorScroll duration

While going through the AngularJS documentation, I have not been able to determine whether $anchorScroll has a duration or easing option for smoother scrolling to elements. The documentation only shows: $location.hash('bottom'); // call $ancho ...

Dealing with text changes in JQuery inputs: best practices

Although this question has been raised numerous times, a definitive answer remains elusive. My goal is to trigger a process (such as an ajax call) when a user types into an input field. Initially, I attempted to utilize the onchange event, but it failed to ...

What is the best way to send a value to a controller function with Ajax when a button is clicked, and then immediately redirect to a new page?

One of my challenges involves a button labeled Next that triggers a page change: <a class="disabled" href="step"> <button class="btn btn-primary launch-btn disabled next">NEXT</button> </a> Upon clicking this button, I have an ...

Update the Parent CRM Page upon closing the child ASP.net page

This pertains to CRM 2016 on-premise implementation. In my CRM 2016 environment, I have a customized button in the command bar that utilizes a JavaScript web resource to invoke an external asp.net page hosted on another server. This external page facilita ...

Unable to halt a timer running on an interval, clearInterval() shows its limitations

Today is the first time I am experimenting with using clearInterval(). After exploring various examples and studying the interval documentation, it seems like this function is crucial for stopping an interval. However, I must be missing something. The mai ...

Extract the body.req object within a loop in a Node.js application

I'm looking to efficiently parse and save the body of a POST request using Mongoose in Node.js. Is there a way to use a for loop to accomplish this task, rather than manually saving every property? My ideal solution would involve something like: for ...

Tips for concealing a div when the inner HTML contains a particular string

Here is the current content I am working with: <div class="showcaseBeds">0 Bedroom</div> <div class="showcaseBaths">0.00 Total Bathroom</div> <div class="IDX-showcaseBeds">3 Bedrooms</div> <div class="IDX-showcas ...