Vue.js / How to access the function in another component












0














I just started learning Vue.js. I need to call the function in another component in my project. When I add new data to the table with createAnnouncement.vue, I want to go into announcement.vue and call the queryAnnouncement function. How can I do that? I would appreciate if you could help, please explain with a sample. Or edit my codes.



Announcement.Vue template:



    <template>
// more div or not important template code
<div class="dataTables_filter" style="margin-bottom:10px">
<label>
<input class="form-control form-control-sm" placeholder="Search" aria-controls="m_table_1" type="search" v-model="searchField" @change="filter()">
</label>
<a style="float:right" href="#" data-target="#create-announcement-modal" data-toggle="modal" class="btn btn-primary">
<i class="">Add</i>&nbsp;
</a>
</div>

// more div or not important template code
</template>


Announcement.Vue Script Code:



<script>
import toastr from "toastr";
export default {
name: 'announcement',
data() {
return {
announcements: ,
searchField: "",
deleteCsrfToken: this.$root.csrfTokens["deleteAnnouncement"]
}
},
beforeMount: async function () {
await this.queryAnnouncements();
},
methods: {
filter: async function () {
await this.queryAnnouncements(this.searchField);
},
queryAnnouncements: async function (filter, pageSize, pageIndex, sortBy, sortType) {
var data = {
"query[general-filter]": filter,
"pagination[perpage]": !!pageSize ? pageSize : 10,
"pagination[page]": !!pageIndex ? pageIndex : 1,
"sort[field]": sortType,
"sort[sort]": !!sortBy ? sortBy : "asc"
};
let response = await axios
.get("/Announcement/QueryAnnouncements", { params: data })
this.announcements = response.data.data;
},
}
}




createAnnouncement.vue code:



  <template>
<button @click="createAnnouncement()" v-bind:disabled="contentDetail === ''" class="btn btn-success">
Save</button>

//not important template codes
<template>
<script>
import toastr from "toastr";
export default {
name: 'create-announcement',
data() {
return {
contentDetail: "",
createCsrfToken: this.$root.csrfTokens["createAnnouncement"],
}
},
methods: {
createAnnouncement: async function () {
var self = this;
var data = {
content: this.contentDetail,
};
let response = await axios
.post("/Announcement/createAnnouncement",
data,
{
headers: {
RequestVerificationToken: self.createCsrfToken
}
})
if (response.status == 200) {
$("#create-announcement-modal .close").click()
$("#create-announcement-form").trigger('reset');
toastr["success"]("Kayıt başarıyla eklendi!", "Başarılı!");
self.contentDetail = "";
}
else {
toastr["warning"]("Hata", "Kayıt Eklenemedi.");
}

}
},

}
</script>


Please show with sample or arrangement, my english is not very good. Thanks.










share|improve this question
























  • please follow this article medium.com/@andrejsabrickis/…
    – Boussadjra Brahim
    Nov 11 at 20:54










  • How do I run the function after adding an announcement? Please show with sample @BoussadjraBrahim
    – Mustafa Sarıdal
    Nov 11 at 21:05










  • you want when you add an announcement in createAnnouncement component to call queryAnnouncements?
    – Boussadjra Brahim
    Nov 11 at 21:08










  • Yes! @BoussadjraBrahim
    – Mustafa Sarıdal
    Nov 11 at 21:17










  • So did you followed the article in the given link above?
    – Boussadjra Brahim
    Nov 11 at 21:24
















0














I just started learning Vue.js. I need to call the function in another component in my project. When I add new data to the table with createAnnouncement.vue, I want to go into announcement.vue and call the queryAnnouncement function. How can I do that? I would appreciate if you could help, please explain with a sample. Or edit my codes.



Announcement.Vue template:



    <template>
// more div or not important template code
<div class="dataTables_filter" style="margin-bottom:10px">
<label>
<input class="form-control form-control-sm" placeholder="Search" aria-controls="m_table_1" type="search" v-model="searchField" @change="filter()">
</label>
<a style="float:right" href="#" data-target="#create-announcement-modal" data-toggle="modal" class="btn btn-primary">
<i class="">Add</i>&nbsp;
</a>
</div>

// more div or not important template code
</template>


Announcement.Vue Script Code:



<script>
import toastr from "toastr";
export default {
name: 'announcement',
data() {
return {
announcements: ,
searchField: "",
deleteCsrfToken: this.$root.csrfTokens["deleteAnnouncement"]
}
},
beforeMount: async function () {
await this.queryAnnouncements();
},
methods: {
filter: async function () {
await this.queryAnnouncements(this.searchField);
},
queryAnnouncements: async function (filter, pageSize, pageIndex, sortBy, sortType) {
var data = {
"query[general-filter]": filter,
"pagination[perpage]": !!pageSize ? pageSize : 10,
"pagination[page]": !!pageIndex ? pageIndex : 1,
"sort[field]": sortType,
"sort[sort]": !!sortBy ? sortBy : "asc"
};
let response = await axios
.get("/Announcement/QueryAnnouncements", { params: data })
this.announcements = response.data.data;
},
}
}




createAnnouncement.vue code:



  <template>
<button @click="createAnnouncement()" v-bind:disabled="contentDetail === ''" class="btn btn-success">
Save</button>

//not important template codes
<template>
<script>
import toastr from "toastr";
export default {
name: 'create-announcement',
data() {
return {
contentDetail: "",
createCsrfToken: this.$root.csrfTokens["createAnnouncement"],
}
},
methods: {
createAnnouncement: async function () {
var self = this;
var data = {
content: this.contentDetail,
};
let response = await axios
.post("/Announcement/createAnnouncement",
data,
{
headers: {
RequestVerificationToken: self.createCsrfToken
}
})
if (response.status == 200) {
$("#create-announcement-modal .close").click()
$("#create-announcement-form").trigger('reset');
toastr["success"]("Kayıt başarıyla eklendi!", "Başarılı!");
self.contentDetail = "";
}
else {
toastr["warning"]("Hata", "Kayıt Eklenemedi.");
}

}
},

}
</script>


Please show with sample or arrangement, my english is not very good. Thanks.










share|improve this question
























  • please follow this article medium.com/@andrejsabrickis/…
    – Boussadjra Brahim
    Nov 11 at 20:54










  • How do I run the function after adding an announcement? Please show with sample @BoussadjraBrahim
    – Mustafa Sarıdal
    Nov 11 at 21:05










  • you want when you add an announcement in createAnnouncement component to call queryAnnouncements?
    – Boussadjra Brahim
    Nov 11 at 21:08










  • Yes! @BoussadjraBrahim
    – Mustafa Sarıdal
    Nov 11 at 21:17










  • So did you followed the article in the given link above?
    – Boussadjra Brahim
    Nov 11 at 21:24














0












0








0







I just started learning Vue.js. I need to call the function in another component in my project. When I add new data to the table with createAnnouncement.vue, I want to go into announcement.vue and call the queryAnnouncement function. How can I do that? I would appreciate if you could help, please explain with a sample. Or edit my codes.



Announcement.Vue template:



    <template>
// more div or not important template code
<div class="dataTables_filter" style="margin-bottom:10px">
<label>
<input class="form-control form-control-sm" placeholder="Search" aria-controls="m_table_1" type="search" v-model="searchField" @change="filter()">
</label>
<a style="float:right" href="#" data-target="#create-announcement-modal" data-toggle="modal" class="btn btn-primary">
<i class="">Add</i>&nbsp;
</a>
</div>

// more div or not important template code
</template>


Announcement.Vue Script Code:



<script>
import toastr from "toastr";
export default {
name: 'announcement',
data() {
return {
announcements: ,
searchField: "",
deleteCsrfToken: this.$root.csrfTokens["deleteAnnouncement"]
}
},
beforeMount: async function () {
await this.queryAnnouncements();
},
methods: {
filter: async function () {
await this.queryAnnouncements(this.searchField);
},
queryAnnouncements: async function (filter, pageSize, pageIndex, sortBy, sortType) {
var data = {
"query[general-filter]": filter,
"pagination[perpage]": !!pageSize ? pageSize : 10,
"pagination[page]": !!pageIndex ? pageIndex : 1,
"sort[field]": sortType,
"sort[sort]": !!sortBy ? sortBy : "asc"
};
let response = await axios
.get("/Announcement/QueryAnnouncements", { params: data })
this.announcements = response.data.data;
},
}
}




createAnnouncement.vue code:



  <template>
<button @click="createAnnouncement()" v-bind:disabled="contentDetail === ''" class="btn btn-success">
Save</button>

//not important template codes
<template>
<script>
import toastr from "toastr";
export default {
name: 'create-announcement',
data() {
return {
contentDetail: "",
createCsrfToken: this.$root.csrfTokens["createAnnouncement"],
}
},
methods: {
createAnnouncement: async function () {
var self = this;
var data = {
content: this.contentDetail,
};
let response = await axios
.post("/Announcement/createAnnouncement",
data,
{
headers: {
RequestVerificationToken: self.createCsrfToken
}
})
if (response.status == 200) {
$("#create-announcement-modal .close").click()
$("#create-announcement-form").trigger('reset');
toastr["success"]("Kayıt başarıyla eklendi!", "Başarılı!");
self.contentDetail = "";
}
else {
toastr["warning"]("Hata", "Kayıt Eklenemedi.");
}

}
},

}
</script>


Please show with sample or arrangement, my english is not very good. Thanks.










share|improve this question















I just started learning Vue.js. I need to call the function in another component in my project. When I add new data to the table with createAnnouncement.vue, I want to go into announcement.vue and call the queryAnnouncement function. How can I do that? I would appreciate if you could help, please explain with a sample. Or edit my codes.



Announcement.Vue template:



    <template>
// more div or not important template code
<div class="dataTables_filter" style="margin-bottom:10px">
<label>
<input class="form-control form-control-sm" placeholder="Search" aria-controls="m_table_1" type="search" v-model="searchField" @change="filter()">
</label>
<a style="float:right" href="#" data-target="#create-announcement-modal" data-toggle="modal" class="btn btn-primary">
<i class="">Add</i>&nbsp;
</a>
</div>

// more div or not important template code
</template>


Announcement.Vue Script Code:



<script>
import toastr from "toastr";
export default {
name: 'announcement',
data() {
return {
announcements: ,
searchField: "",
deleteCsrfToken: this.$root.csrfTokens["deleteAnnouncement"]
}
},
beforeMount: async function () {
await this.queryAnnouncements();
},
methods: {
filter: async function () {
await this.queryAnnouncements(this.searchField);
},
queryAnnouncements: async function (filter, pageSize, pageIndex, sortBy, sortType) {
var data = {
"query[general-filter]": filter,
"pagination[perpage]": !!pageSize ? pageSize : 10,
"pagination[page]": !!pageIndex ? pageIndex : 1,
"sort[field]": sortType,
"sort[sort]": !!sortBy ? sortBy : "asc"
};
let response = await axios
.get("/Announcement/QueryAnnouncements", { params: data })
this.announcements = response.data.data;
},
}
}




createAnnouncement.vue code:



  <template>
<button @click="createAnnouncement()" v-bind:disabled="contentDetail === ''" class="btn btn-success">
Save</button>

//not important template codes
<template>
<script>
import toastr from "toastr";
export default {
name: 'create-announcement',
data() {
return {
contentDetail: "",
createCsrfToken: this.$root.csrfTokens["createAnnouncement"],
}
},
methods: {
createAnnouncement: async function () {
var self = this;
var data = {
content: this.contentDetail,
};
let response = await axios
.post("/Announcement/createAnnouncement",
data,
{
headers: {
RequestVerificationToken: self.createCsrfToken
}
})
if (response.status == 200) {
$("#create-announcement-modal .close").click()
$("#create-announcement-form").trigger('reset');
toastr["success"]("Kayıt başarıyla eklendi!", "Başarılı!");
self.contentDetail = "";
}
else {
toastr["warning"]("Hata", "Kayıt Eklenemedi.");
}

}
},

}
</script>


Please show with sample or arrangement, my english is not very good. Thanks.







vue.js vuejs2 components






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 21:03









Boussadjra Brahim

5,3523631




5,3523631










asked Nov 11 at 20:45









Mustafa Sarıdal

41




41












  • please follow this article medium.com/@andrejsabrickis/…
    – Boussadjra Brahim
    Nov 11 at 20:54










  • How do I run the function after adding an announcement? Please show with sample @BoussadjraBrahim
    – Mustafa Sarıdal
    Nov 11 at 21:05










  • you want when you add an announcement in createAnnouncement component to call queryAnnouncements?
    – Boussadjra Brahim
    Nov 11 at 21:08










  • Yes! @BoussadjraBrahim
    – Mustafa Sarıdal
    Nov 11 at 21:17










  • So did you followed the article in the given link above?
    – Boussadjra Brahim
    Nov 11 at 21:24


















  • please follow this article medium.com/@andrejsabrickis/…
    – Boussadjra Brahim
    Nov 11 at 20:54










  • How do I run the function after adding an announcement? Please show with sample @BoussadjraBrahim
    – Mustafa Sarıdal
    Nov 11 at 21:05










  • you want when you add an announcement in createAnnouncement component to call queryAnnouncements?
    – Boussadjra Brahim
    Nov 11 at 21:08










  • Yes! @BoussadjraBrahim
    – Mustafa Sarıdal
    Nov 11 at 21:17










  • So did you followed the article in the given link above?
    – Boussadjra Brahim
    Nov 11 at 21:24
















please follow this article medium.com/@andrejsabrickis/…
– Boussadjra Brahim
Nov 11 at 20:54




please follow this article medium.com/@andrejsabrickis/…
– Boussadjra Brahim
Nov 11 at 20:54












How do I run the function after adding an announcement? Please show with sample @BoussadjraBrahim
– Mustafa Sarıdal
Nov 11 at 21:05




How do I run the function after adding an announcement? Please show with sample @BoussadjraBrahim
– Mustafa Sarıdal
Nov 11 at 21:05












you want when you add an announcement in createAnnouncement component to call queryAnnouncements?
– Boussadjra Brahim
Nov 11 at 21:08




you want when you add an announcement in createAnnouncement component to call queryAnnouncements?
– Boussadjra Brahim
Nov 11 at 21:08












Yes! @BoussadjraBrahim
– Mustafa Sarıdal
Nov 11 at 21:17




Yes! @BoussadjraBrahim
– Mustafa Sarıdal
Nov 11 at 21:17












So did you followed the article in the given link above?
– Boussadjra Brahim
Nov 11 at 21:24




So did you followed the article in the given link above?
– Boussadjra Brahim
Nov 11 at 21:24

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253063%2fvue-js-how-to-access-the-function-in-another-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253063%2fvue-js-how-to-access-the-function-in-another-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini