首页 \ 问答 \ 访问自定义组件数据/方法(Accessing Custom Component Data/Methods)

访问自定义组件数据/方法(Accessing Custom Component Data/Methods)

我有一个使用自定义组件的html文件。 自定义组件伸出并获取bind()方法的数据。 因此,当组件绑定时,它会获取数据并相应地设置属性。 该组件还有一个Save()方法,在调用时,应该将对象提交给数据库。

现在,在我的外部html文件中,我已导入此自定义组件。 所以我有自定义组件,然后我有一个提交按钮(不是自定义组件的一部分),如下所示:

<custom-component></custom-component>
<button click.trigger="submitCustomComponentData()"></button>

我在自定义组件视图中没有按钮的原因是因为该视图并不总是具有相同的按钮,这使得组件不可扩展。

submitCustomComponentData()方法基本上调用组件VM中的更新方法。

现在,当页面加载时,一切都运行完美。 数据被拉入,我的所有输入都预先填充了以前的数据(来自数据库)。 一切都很好。 但是,当我调用submitCustomComponentData()方法(或单击按钮)时,我收到一个错误,因为没有填充该对象。 这就像我失去了实例或其他东西。

以下是一些重要部分的片段:

这是我的外部html文件的样子。 它由自定义组件组成。

<template>
    <require from="resources/components/dispatch-questions/dispatch-questions"></require>

<section class="pages au-animate">
    <div class="row" id="dispatch-questions">
        <dispatch-questions></dispatch-questions>
    </div>
</section>

</template>

并且这个虚拟机注入了dispatch-questions组件,如下所示:

constructor(private router: Router, private dq: DispatchQuestions) {
    }

它还有一个click.trigger方法,该方法应该调用组件中的updateDB方法。 此时,组件(应该已经具有在bind()上创建的相同实例)应该将该对象提交给DB。

但我收到一个错误,因为某些原因,该对象是空的。 组件中的函数是抓取this.myObject并将其提交给DB。 我想当我从外部VM(而不是组件VM)调用更新功能时,我正在丢失组件的this实例。 我认为这就是问题。如果这是问题,我们不确定如何修复它。 任何帮助都是极好的!

我试图在Gist上创建一个简单的版本。 https://gist.run/?id=f07b2eaae9bec27acda296189585ea6c


I have an html file that uses a custom component. The custom component reaches out and gets data on the bind() method. So, when the component gets bound, it gets the data and sets the properties accordingly. This component also has a Save() method that, when called, should submit the object to the database.

Now, in my outer html file, I have imported this custom component. So I have the custom component and then I have a submit button (not a part of the custom component) like this:

<custom-component></custom-component>
<button click.trigger="submitCustomComponentData()"></button>

The reason I don't have the buttons in the custom component view is because that view isn't always going to have the same buttons, which then makes the component non-extendable.

The submitCustomComponentData() method basically calls the update method that is in my component VM.

Now, when when the page loads, everything runs perfect. The data gets pulled in, all of my inputs are pre-populated with the previous data (from the DB). Everything is great. However, when I call the submitCustomComponentData() method (or click the button), I get an error because the object isn't populated. It's like I'm losing the instance or something.

Here is a snippet of some important parts:

This is what my outer html file looks like. It's made up of the custom component.

<template>
    <require from="resources/components/dispatch-questions/dispatch-questions"></require>

<section class="pages au-animate">
    <div class="row" id="dispatch-questions">
        <dispatch-questions></dispatch-questions>
    </div>
</section>

</template>

And the VM for this gets injected with the dispatch-questions component like so:

constructor(private router: Router, private dq: DispatchQuestions) {
    }

It also has a click.trigger method that SHOULD call the updateDB method that is in the component. At that moment, the component (which should already have the same instance that got created on bind()) should submit that object to the DB.

But I'm getting an error because for some reason, the object is empty. The function in the component is grabbing this.myObject and submitting it to the DB. I think when I'm calling the update function from my outer VM (not the component VM) I'm losing the component's this instance. I think that's the problem..Not sure how to fix it IF that's the issue. Any help would be awesome!

I've tried to create a simple version on Gist. https://gist.run/?id=f07b2eaae9bec27acda296189585ea6c


原文:https://stackoverflow.com/questions/39670825
更新时间:2023-07-25 16:07

最满意答案

虽然我通常不喜欢将更长的代码转储到SO上,但这次它对你来说仍然有些帮助。

这是我自己多年来使用的功能:

/// <summary>
/// Splits the file name if it contains an executable AND an argument.
/// </summary>
public static void CheckSplitFileName( ref string fileName, ref string arguments )
{
    if ( !string.IsNullOrEmpty( fileName ) )
    {
        if ( fileName.IndexOf( @"http://" ) == 0 ||
            fileName.IndexOf( @"https://" ) == 0 ||
            fileName.IndexOf( @"ftp://" ) == 0 ||
            fileName.IndexOf( @"file://" ) == 0 )
        {
            // URLs not supported, skip.
            return;
        }
        if ( File.Exists( fileName ) )
        {
            // Already a full path, do nothing.
            return;
        }
        else if ( Directory.Exists( fileName ) )
        {
            // Already a full path, do nothing.
            return;
        }
        else
        {
            // Remember.
            string originalFileName = fileName;

            if ( !string.IsNullOrEmpty( fileName ) )
            {
                fileName = fileName.Trim();
            }

            if ( !string.IsNullOrEmpty( arguments ) )
            {
                arguments = arguments.Trim();
            }

            // --

            if ( string.IsNullOrEmpty( arguments ) &&
                !string.IsNullOrEmpty( fileName ) && fileName.Length > 2 )
            {
                if ( fileName.StartsWith( @"""" ) )
                {
                    int pos = fileName.IndexOf( @"""", 1 );

                    if ( pos > 0 && fileName.Length > pos + 1 )
                    {
                        arguments = fileName.Substring( pos + 1 ).Trim();
                        fileName = fileName.Substring( 0, pos + 1 ).Trim();
                    }
                }
                else
                {
                    int pos = fileName.IndexOf( @" " );
                    if ( pos > 0 && fileName.Length > pos + 1 )
                    {
                        arguments = fileName.Substring( pos + 1 ).Trim();
                        fileName = fileName.Substring( 0, pos + 1 ).Trim();
                    }
                }
            }

            // --
            // Possibly revert back.

            if ( !string.IsNullOrEmpty( fileName ) )
            {
                string s = fileName.Trim( '"' );
                if ( !File.Exists( s ) && !Directory.Exists( s ) )
                {
                    fileName = originalFileName;
                }
            }
        }
    }
}

我正在以下列方式使用它:

var fileName = textBox1.Text.Trim();
var arguments = string.Empty;
CheckSplitFileName( ref fileName, ref arguments );

然后,将其传递给ProcessStartupInfo类:

var info = new ProcessStartInfo();
info.FileName = fileName;
info.Arguments = arguments;

While I usually dislike when a longer code is dumped on SO, maybe this time it is still somewhat helpful for you.

This is my own function I'm using since years to split:

/// <summary>
/// Splits the file name if it contains an executable AND an argument.
/// </summary>
public static void CheckSplitFileName( ref string fileName, ref string arguments )
{
    if ( !string.IsNullOrEmpty( fileName ) )
    {
        if ( fileName.IndexOf( @"http://" ) == 0 ||
            fileName.IndexOf( @"https://" ) == 0 ||
            fileName.IndexOf( @"ftp://" ) == 0 ||
            fileName.IndexOf( @"file://" ) == 0 )
        {
            // URLs not supported, skip.
            return;
        }
        if ( File.Exists( fileName ) )
        {
            // Already a full path, do nothing.
            return;
        }
        else if ( Directory.Exists( fileName ) )
        {
            // Already a full path, do nothing.
            return;
        }
        else
        {
            // Remember.
            string originalFileName = fileName;

            if ( !string.IsNullOrEmpty( fileName ) )
            {
                fileName = fileName.Trim();
            }

            if ( !string.IsNullOrEmpty( arguments ) )
            {
                arguments = arguments.Trim();
            }

            // --

            if ( string.IsNullOrEmpty( arguments ) &&
                !string.IsNullOrEmpty( fileName ) && fileName.Length > 2 )
            {
                if ( fileName.StartsWith( @"""" ) )
                {
                    int pos = fileName.IndexOf( @"""", 1 );

                    if ( pos > 0 && fileName.Length > pos + 1 )
                    {
                        arguments = fileName.Substring( pos + 1 ).Trim();
                        fileName = fileName.Substring( 0, pos + 1 ).Trim();
                    }
                }
                else
                {
                    int pos = fileName.IndexOf( @" " );
                    if ( pos > 0 && fileName.Length > pos + 1 )
                    {
                        arguments = fileName.Substring( pos + 1 ).Trim();
                        fileName = fileName.Substring( 0, pos + 1 ).Trim();
                    }
                }
            }

            // --
            // Possibly revert back.

            if ( !string.IsNullOrEmpty( fileName ) )
            {
                string s = fileName.Trim( '"' );
                if ( !File.Exists( s ) && !Directory.Exists( s ) )
                {
                    fileName = originalFileName;
                }
            }
        }
    }
}

I'm using it in the following way:

var fileName = textBox1.Text.Trim();
var arguments = string.Empty;
CheckSplitFileName( ref fileName, ref arguments );

Then, pass it to the ProcessStartupInfo class:

var info = new ProcessStartInfo();
info.FileName = fileName;
info.Arguments = arguments;

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)